Quote:
Originally Posted by TheLinuxer
How good is Linux's memory management (MM) in this version? would it be a good idea to develop a thin, simple and robust layer of memory management in my application on top of Linux MM, since my app allocs and deallocs a bunch (a high number) of pieces of memory of similar size (not exact the same size but close)? Performance is critical in my app.
|
The memory-management code that your
application invokes are actually provided by the runtime libraries used by your application, such as
glibc. These libraries handle the complexities of
malloc() and
free(),
et al, calling upon the kernel only to obtain additional (large) chunks of memory as-needed. The
kernel is not bothered with trifles.
If you have a high-performance memory requirement like this, consider implementing your own layer of code within your appliation. Handle all memory-allocation requirements by calling this layer.
Consider implementing a "free list," one for each chunk-size. When you release a memory block, just fill it with garbage bytes and chain it to the appropriate free-list. The next request for the same-sized block can check to see if the free-list has anything on it. If so, the first block is dequeued, set to binary zeroes, and returned. A LIFO (last-in first-out) strategy is used to increase the probability that the necessary memory-pages will not have been swapped out yet; the address is "recently used."
If you have many memory allocation requests of a similar size, round them up to a convenient larger size. In the long run, a few extra bytes won't matter. Then use the free-list strategy outlined above.
In the free-list strategy, you never check any other list when satisfying memory requests. You don't "clean up" the lists, or get excited about how many entries they might contain.
I find it
very helpful to "fill released blocks with garbage," so that if I accidentally refer to it later the problem is very noticeable. (Finding garbage-bytes in a released block is also an indication of the dreaded "double free.") I also find it very helpful to set the newly allocated (or re-used) blocks explicitly to binary zeroes. Adding a few extra (known-zero) bytes to every memory allocation is also a useful idea.