{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreicn7sajmbgn2quwe5quzldwjdvek6rhmluiefm3vz4s3357eok6tu",
    "uri": "at://did:plc:pgryn3ephfd2xgft23qokfzt/app.bsky.feed.post/3mi6uqhdn2fm2"
  },
  "path": "/t/found-the-fix-for-memory-not-being-freed-when-switching-models-on-linux-its-not-python-or-pytorch/174750#post_2",
  "publishedAt": "2026-03-29T06:48:21.000Z",
  "site": "https://discuss.huggingface.co",
  "textContent": "Interesting finding, but from reading the doc, I don’t see why glibc should be using `sbrk` to satisfy these allocations.\n\n>\n>                   The upper limit\n>                   is DEFAULT_MMAP_THRESHOLD_MAX: [...] 4*1024*1024*sizeof(long) on 64-bit systems.\n>\n>                   Note: Nowadays, glibc uses a dynamic mmap threshold by\n>                   default.  The initial value of the threshold is 128*1024,\n>                   but when blocks larger than the current threshold and less\n>                   than or equal to DEFAULT_MMAP_THRESHOLD_MAX are freed, the\n>                   threshold is adjusted upward to the size of the freed\n>                   block.  When dynamic mmap thresholding is in effect, the\n>                   threshold for trimming the heap is also dynamically\n>                   adjusted to be twice the dynamic mmap threshold.  Dynamic\n>                   adjustment of the mmap threshold is disabled if any of the\n>                   M_TRIM_THRESHOLD, M_TOP_PAD, M_MMAP_THRESHOLD, or\n>                   M_MMAP_MAX parameters is set.\n>\n>\n\nOn x86-64, sizeof(long) is 4, so any allocation larger than 16 MiB should be over the limit and thus be satisfied with mmap, right? Unless it happens to fit within some already-allocated arena’s free list, but in that case it wouldn’t increase the process’s size.\n\nEvidently _something_ with Torch’s allocation pattern interacts pathologically, but I’m not sure what.\n\nThat said, why is Torch even mallocing memory to store the model, rather than simply mmapping the model file into the process’s address space? That would avoid the whole issue and also save a pointless memory copy.",
  "title": "Found the fix for memory not being freed when switching models on Linux (it's not Python or PyTorch)"
}