文法項目:なくはない
一ノ瀬:好きじゃない。でも、好きじゃなくもない。
Implementing resource management within software has been a problem faced by developers for decades. Conventions such Resource Allocation is Initialization (RAII) and language features such as Rust’s ownership concept are attempts at addressing the resource management problem.
Deciding between returning a byte[] or Stream object from a C# method requires thoughtful consideration. There is a tradeoff between efficient memory use and resource management safety. Are Stream objects better than byte[] objects when passing around file content within a software system? As happens frequently in engineering efforts, the better solution depends on the problem’s conditions.
In situations where average file sizes are a considerable percentage of total system memory, such as 500MB files on a system with 4GB of RAM, there is a strong case for using Stream objects and accepting the responsibility of resource management as opposed to allocating large chunks of memory. By using Stream objects, the developer is responsible for ensuring timely disposal of resources and release of file handles. The developer is responsible for preventing situations where freed resources are used and where resources are freed multiple times or never at all.
Similarly, where average file sizes are less than 1024 bytes, there is a strong case for using byte[] and trusting the runtime with resource management and garbage collection. byte[] objects do not implement IDisposable, and the environment is responsible for managing their resources.
The choice between byte[] and Stream objects is more interesting when the average size of files being manipulated is between the above extremes. Theoretically, 4000 1MB files can be loaded into memory on a system with 4GB of RAM. The decision between using byte[] or Stream objects depends on the software’s typical usage. If the software typically manipulates hundreds of thousands of such files concurrently, Stream objects will likely be the better option.
But, should Stream objects be used if the system manipulates a couple hundred 1MB files concurrently in the worst case?
The existence of the following exceptions exemplifies the persistent difficulty of resource management even in an environment that provides garbage collection:
I prefer a safe approach that depends on the software environment and runtime over a risky idealized general solution that requires developer effort to manage resources. I prefer a fitted solution rather than one that is over-engineered. On a system with 4GB of memory, I prefer an implementation where 200MB of memory is allocated in the worst case to store file content with automatic resource management rather than an implementation where 2MB of memory is used but requires developer diligence. In this situation, I prefer byte[] objects over Stream objects.
In an effort to develop and maintain skills, I read the following books in the last three months:
Pi Configuration Procedures (on Raspberry Pi Zero W):
dtoverlay=dwc2
otg_mode
.rootwait
, surrounded by spaces:modules-load=dwc2,g_ether
/boot/ssh
.Driver Installation Procedures (on Microsoft Windows 11):
Connection Procedures (on Microsoft Windows 11)
ssh myusername@myraspberrypi.local