Memory Allocation

One thing that bugs me about most applications is the variations and sometime unknown memory consumption of applications.  Very often application developers are abusing malloc() or allocating larger chunks of memory with the notion that if the operating system runs out of memory it will swap it out automatically.  Often these abuses result in swapping memory to disk, unpredictable performance hits and memory fragmentation.

I’m thinking to require applications to pre-allocate or pre-define their memory requirements in their .bss ELF section; this memory would be automatically allocated in one block during installation/runtime.  While the standard method is an easy way to simplify application development, I believe this may actually hinder overall performances and predictability.  I see memory management a little bit like disk management; what I mean is, a user would need to review how much free memory he has in his system, and check that the application he wants to install will fit within the available memory.  With memory being affordable and in large quantity this should not restrict too much application development yet this philosophy will ensure the developers will at least think twice about how much memory they really require to accomplish a specific job.

This approach should simplify run-time management and avoid hard to predict performance hits due to having to swap in or out a chunk of memory.  Unfortunately this solution also means there could be some “wasted” memory; space reserved for a specific task that the user may not actually perform under a set of parameters or configurations.

To summarize, the benefits are:

  • Predictable performances (no performance hit due to swapping in or out)
  • No memory fragmentation
  • Known pre-defined memory requirements for a given configuration

The negatives are:

  • Additional planification required by the developers
  • Allocated but unused memory under some usage patterns in non-customized applications
  • Restriction in the amount of simultaneously used/installed applications

For applications which would require a large amount of memory (more than most systems would be able to handle); a built-in custom swapping system could probably be implemented.  This may require additional work for the developer although the benefits would be a most likely better tuned and better controled swapping system that could be customized for the task at hand.

Whether the above method of memory management is implemented or not, the minimum memory allocation size should be the largest of the data or code cache line size.  If memory is allocated in smaller blocks it can seriously hinder multiprocessor performance when two processors are writing to the same cache line.  Details on detecting cache line size are here and a reference memory allocator implementation called Hoard is also available.