Overview
top -o RES
Memory Leak
- new() without delete
- Keep entries that are no longer needed
- Missing virtual ~Base()
- Circular reference
Memory leaks: Tools
bslma::TestAllocator
Advantages:
- fast
- small overhead
- scoped
Disadvantages:
- code changes required
- compile and link required
Address Sanitizer
Advantages:
- no code change required
- fast
Disadvantages:
- compile and link required
- extra memory cost
Valgrind (memcheck and massif)
Advantages:
- no code change required
- no compile and link required
Disadvantages:
- slow (10-30 times slower)
- extra memory cost
Memory leaks: Tips
- “Static” leaks may hide the real issue - we need enough traffic for profiling
- They are all good tools, but for different cases
- Catch the problem in earlier stages - Integrate Address Sanitizer in CI
- Install the tools so we can start profiling easily
- Care about the lifecycle and ownership of what we allocate
Fragmentation
External fragmentation
- I have free spaces, but I need to extend the heap
1 - LargestAllocableBlock / TotalFreeMemory
- Most allocations can be done with chunks in bins
Internal fragmentation
- I allocate a large chunk for a small size
1 - AccessedBytes / TotalAllocatedBytes
- Avoid useless and underused allocations
- Total bytes
- Total in-use allocations
- Total free space
- Largest allocable block
Valgrind DHAT: a dynamic heap analysis tool
Defragmentation
C++
- CppCon 2017: John Lakos “Local (‘Arena’) Memory Allocators (part 1 of 2)”
- CppCon 2017: John Lakos “Local (‘Arena’) Memory Allocators (part 2 of 2)”
malloc
OS
- Buddy memory allocation
- Buddy System – Memory Allocation Technique
- CppCon 2019: Emery Berger “Mesh: Automatically Compacting Your C++ Application’s Memory”
Long-running stateless system
- start and run for a long time
- receive requests and process them, no state
Reference
- https://www.youtube.com/watch?v=y6AN0ks2q0A
- Allocator-Aware C++ Type Design - Jonathan Coe - C++ on Sea 2024
- https://github.com/jbcoe/value_types
- https://github.com/jbcoe/allocators
- What Programmers Should Know About Memory Allocation - S. Al Bahra, H. Sowa, P. Khuong - CppCon 2019
- Getting Allocators out of Our Way - Alisdair Meredith & Pablo Halpern - CppCon 2019