Rendered at 23:48:15 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
kinow 9 hours ago [-]
A short post, but it's every easy to read. Great job Daniel. Looking forward to more posts about this.
One recent case we had is that a developer noticed the memory wasn't being returned to the OS, and sent a pull request that forces `malloc_trim`. There are some Python projects that also do this call periodically, but while it works for `malloc` allocated objects, it doesn't work jemalloc, mimalloc (default for thread-free builds, if I'm not mistaken). It doesn't break either, but still...
The `malloc_trim` does seem to help producing a better memory usage pattern, but it's accompanying a `gc.collect`, so I'm being cautious and asking for more tests/benchmarks/etc.
Another developer has found a dictionary that seems to be the actual source of our problem, so we may not have to add `malloc_trim`, but maybe that'd be something interesting for a future post if you haven't already dug into that direction :)
Someone 8 hours ago [-]
So, Python tries to outsmart the system’s malloc. Isn’t there a risk there that the two interact in unexpected ways?
For example, it seems a realloc of an object that is larger than 512 bytes to a size smaller than 512 bytes may allocate a megabyte of memory (https://docs.python.org/3/c-api/memory.html#the-pymalloc-all...: “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 either 256 KiB on 32-bit platforms or 1 MiB on 64-bit platforms“
One recent case we had is that a developer noticed the memory wasn't being returned to the OS, and sent a pull request that forces `malloc_trim`. There are some Python projects that also do this call periodically, but while it works for `malloc` allocated objects, it doesn't work jemalloc, mimalloc (default for thread-free builds, if I'm not mistaken). It doesn't break either, but still...
The `malloc_trim` does seem to help producing a better memory usage pattern, but it's accompanying a `gc.collect`, so I'm being cautious and asking for more tests/benchmarks/etc.
Another developer has found a dictionary that seems to be the actual source of our problem, so we may not have to add `malloc_trim`, but maybe that'd be something interesting for a future post if you haven't already dug into that direction :)
For example, it seems a realloc of an object that is larger than 512 bytes to a size smaller than 512 bytes may allocate a megabyte of memory (https://docs.python.org/3/c-api/memory.html#the-pymalloc-all...: “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 either 256 KiB on 32-bit platforms or 1 MiB on 64-bit platforms“