Dynamic Memory Price
13 Jul 2021
[
c++
memory
cache
allocation
performance
]
Principles of data organization for C++ developers
Check memory allocation patterns
Upfront (embedded and real time OS)
Allocation in bursts (introduce caching, custom allocator for STL) use good allocator
Gradual allocation - use good allocator
Why allocators are slow?
Allocator - finding the chunk of available memory is not easy task.
This makes them slow.
The longer running - the more problem. Memory fragmentation.
This also leads to allocation failures.
How to tackle memory fragmentation?
Use vectors of objects instead of vectors of pointers
Occasionally restart your program
Preallocate all the needed memory at the program beginning
Cache memory chunks. Example - producer/consumer
Use special memory allocations that promise low fragmentation
Custom allocators for STL containers
Allocator implements allocate and deallocate methods
Excellent choice when multiple small allocations are performed
Caching memory chunks: Checklist
Many small chunks or few larger?
Are allocation and deallocation in the same order?
Is allocation in one thread and deallocation in another?
Is program a long running one?
Criteria for custom allocator:
allocation speed
memory consumption
memory fragmentation
locality of reference
Allocators of Linux - measure speed and consumption
Presentation
Links