Linux heap - does a ton of new / deletes work fine, or does the heap become very fragmented?

I don't know how a bunch of Linux stands out.

I call malloc () / free () many times per second, always with the same dimensions (there are about 10 structures, each fixed size). Besides init time, none of my memory is allocated for long periods of time.

Is this a bad form with standard heaps? (I'm sure someone will ask, β€œWhat kind of heap are you using?” - β€œWow. Standard static heap” .. meaning, I'm not sure.)

Do I have to use a free list or does the heap allow a lot of the same distributions. I am trying to balance readability with performance.

Any tools to help me measure?

+6
source share
3 answers

Valgrind has a special Massif tool for measuring memory usage. This should help profile heap allocations.

+4
source

First of all, if you do not have a measured problem with memory usage, don't even think about using a custom allocator. This is one of the worst forms of premature optimization.

At the same time, even if you have problems, a better solution than a custom allocator will determine why you allocate and free objects so much, and fix the design problem that causes it.

To solve your specific question, the glibc allocator is based on the dlmalloc algorithm, which is almost optimal when it comes to fragmentation. The only way you get it for poorly fragmented memory is the inevitable way: by placing objects with radically different lifetimes in alternation, for example. highlighting a large number of objects, but only freeing each other. I think it will be difficult for you to develop a distribution pattern that will give worse overall memory usage than pools ...

+11
source

I think the best performance optimization is to avoid heap allocation wherever possible (and reasonably). Whenever an object is allocated a stack, the compiler simply moves the stack pointer up, instead of trying to find free space or return the allocated memory to some free list.

How is the life of your structures determined? If you can express the lifetime of an object on a scale, it will really increase productivity.

0
source

Source: https://habr.com/ru/post/896043/


All Articles