Why is the first C++ (m)allocation always 72 KB?
6 hours ago
- #Memory Allocation
- #C++
- #Exception Handling
- The first C++ allocation is always 72 KB due to the C++ standard library setting up an 'emergency pool' for exception handling.
- This emergency pool ensures memory is available for exceptions even if malloc fails, allocated lazily at program startup.
- The size (72 KB) is calculated based on system wordsize (8 bytes on 64-bit) and predefined constants (EMERGENCY_OBJ_SIZE and EMERGENCY_OBJ_COUNT).
- The emergency pool can be tuned or disabled via GLIBCXX_TUNABLES environment variable or libstdc++ build configurations.
- Valgrind previously reported this allocation as 'still reachable', leading to confusion, but newer versions explicitly free it during cleanup.
- Different versions of libstdc++ or compiler flags may result in varying allocation sizes or behaviors for the emergency pool.