python 内存管理

python 内存接口分层图

Raw memory interface -> memory interface (pymalloc allocator) -> object allocators

The pymalloc allocator

Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short lifetime. It uses memory mappings called “arenas” with a fixed size of 256 KB. It falls back to PyMem_RawMalloc() and PyMem_RawRealloc() for allocations larger than 512 bytes.

  1. 大内存分配使用raw memory

  2. 小内存使用arenas 内存池管理

Arenas and pools

The arena is a chunk of 256kB memory allocated on the heap, which provides memory for 64 pools.

python 内存管理_第1张图片
屏幕快照 2018-12-04 下午3.19.30.png

Allocation statistics

  1. You can get allocations statistics by calling sys._debugmallocstats()

  2. PYTHONMALLOCSTATS 使用 环境变量PYTHONMALLOCSTATS=true

你可能感兴趣的:(python 内存管理)