# Allocating memory in a separate pool, and freeing it

**URL:** <https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921>\
**Category:** help\
**Created:** [June 24, 2026, 4:17pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921 "2026-06-24T16:17:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkPhoenix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darkphoenix/32/47022_2.png) [@DarkPhoenix](https://users.rust-lang.org/u/DarkPhoenix)\
**Post date:** [June 24, 2026, 4:17pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/1 "2026-06-24T16:17:23Z")

</div>

I am writing a library (using nightly rust). Its high-level interface is async, with 2 threadpools for various calculation-intensive tasks. It is supposed to be used on servers (with HTTP interface) and in downloadable applications (rust async interface).

One of tasks iran in a threadpool is a data processing task. When a user makes request to do something and provides previously unseen data, a cache generation process begins. It reads data, and extracts small bits necessary for the lib to function. This process can run from time to time, but is not expected to run often.

Depending on implementation, it spikes memory use of the process (http interface to the lib) to 600-1300 MB. Outside of it, process does not need more than 50-200 MB.

The issue with it, is that this memory is not returned to the system by the default allocator. After the cached data generation is complete, memory use stays at 1G+. I tried calling `malloc_trim` after it, and it helps when it comes to memory. However, info everywere suggests I shouldn't be doing that in a lib, so I am looking for different solutions.

One of ideas is to have generation-involved data allocated in a separate memory region, and then free it up as soon as the generation is complete. Is it something doable in rust? Do I need to code my own allocator for that? Or can I use an "instance" of some already coded allocator, with its lifetime being tied to the data generation process (data generation is done - memory is released back to the OS)?

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kpreid/32/22420_2.png) [@kpreid](https://users.rust-lang.org/u/kpreid)\
**Post date:** [June 24, 2026, 4:22pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/2 "2026-06-24T16:22:44Z")

</div>

> [@DarkPhoenix](#):
>
> The issue with it, is that this memory is not returned to the system by the default allocator. After the cached data generation is complete, memory use stays at 1G+. I tried calling `malloc_trim` after it, and it helps when it comes to memory. However, info everywere suggests I shouldn't be doing that in a lib, so I am looking for different solutions.

Your library can provide a notification that it’s done with the heavy memory usage, and the _application_ can choose to call `malloc_trim()` or take other actions in reaction to this or not.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [June 24, 2026, 4:26pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/3 "2026-06-24T16:26:05Z")

</div>

> [@DarkPhoenix](#):
>
> The issue with it, is that this memory is not returned to the system by the default allocator.

Is that just a behaviour of the _default_ allocator?

If it's something solvable by the binary picking a "better" allocator, then it seems fine.

(After all, if this gets used a bunch then being able to re-use that memory for the next unseen data that seems fine?)

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [June 24, 2026, 4:42pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/4 "2026-06-24T16:42:39Z")

</div>

> [@scottmcm](#):
>
> Is that just a behaviour of the _default_ allocator?

Any performant memory allocator will hold onto freed memory for a while in case you need it again soon. Some allocators expose a functions like malloc\_trim to immediately release such cached memory back to the kernel.

> [@scottmcm](#):
>
> (After all, if this gets used a bunch then being able to re-use that memory for the next unseen data that seems fine?)

The point of malloc\_trim is to call it when you know you won't need this memory again soon while the heuristics of the memory allocator might not realize this.

---

<div class="post-metadata">

**Author:** ![DarkPhoenix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darkphoenix/32/47022_2.png) [@DarkPhoenix](https://users.rust-lang.org/u/DarkPhoenix)\
**Post date:** [June 24, 2026, 4:52pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/5 "2026-06-24T16:52:52Z")

</div>

I know it is a possibility. However, it is not without downsides:

- it increases API complexity
- it is a platform-specific function (code is supposed to work on windows too) and allocator-specific function (should I switch global allocator to something else, it will cease to function)
- it frees up all the memory currently not in use for all parts of the process. I do not exactly want to do that, I just do not want use of transient data structures specific to seldomly run generation process to permanently increase it.

So, yes, it is an option, but I am looking for better options.

---

<div class="post-metadata">

**Author:** ![DarkPhoenix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darkphoenix/32/47022_2.png) [@DarkPhoenix](https://users.rust-lang.org/u/DarkPhoenix)\
**Post date:** [June 24, 2026, 4:59pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/6 "2026-06-24T16:59:45Z")

</div>

> [@scottmcm](#):
>
> If it's something solvable by the binary picking a "better" allocator, then it seems fine.

Considering I do not care much about performance of the cache generation process (within reasonable limits of course), I am fine with using an allocator which more aggressively releases memory which was used by the cache generation process' structures.

However, I do not exactly care how it handles memory during cache generation. Default allocator's behavior is fine. I just want to make sure that structures which are temporary to that process are not taking memory anymore (= it is released to the OS). In fact, I wish default allocators had more control over that, not to pull extra dependencies, or code my own allocator (which is something I've never done).

---

<div class="post-metadata">

**Author:** ![yuriy0](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yuriy0/32/30393_2.png) [@yuriy0](https://users.rust-lang.org/u/yuriy0)\
**Post date:** [June 24, 2026, 6:02pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/7 "2026-06-24T18:02:53Z")

</div>

> [@DarkPhoenix](#):
>
> I just want to make sure that structures which are temporary to that process are not taking memory anymore (= it is released to the OS). In fact, I wish default allocators had more control over that, not to pull extra dependencies, or code my own allocator

I don't think the default allocator will give you an guarantees here esp. since its a different implementation on different platforms, and each system allocator will behave differently.

A simple way to do this kind of thing is to allocate a huge chunk of memory up-front using something like [this](https://crates.io/crates/mmap-rs), then use an arena allocator to do your scratch work, then de-allocate the huge chunk. Using this kind of library instead of the system allocator gives you a better chance (but not necessarily a guarantee) that the de-allocation actually lets the OS reclaim the memory.

On Windows, VirtualAlloc/VirtualFree does give the memory back pretty consistently. I don't know of exact details on other systems or whether the library I linked will solve all your needs.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kpreid/32/22420_2.png) [@kpreid](https://users.rust-lang.org/u/kpreid)\
**Post date:** [June 24, 2026, 7:31pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/8 "2026-06-24T19:31:04Z")

</div>

> [@DarkPhoenix](#):
>
> I know it is a possibility. However, it is not without downsides:
> 
> - it increases API complexity

The user of the library would be free to not register for the notification.

> [@](#):
>
> - it is a platform-specific function (code is supposed to work on windows too) and allocator-specific function (should I switch global allocator to something else, it will cease to function)

The point of the notification is that the application author can handle it however fits the platform (and the choice of memory allocator, if they customized it), and the library is _not_ calling platform-specific functions.

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [June 24, 2026, 10:36pm UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/9 "2026-06-24T22:36:12Z")

</div>

`memmap2` might help, I guess? I haven't looked if there is an allocator crate which would provide a nicer interface for the resulting `&mut [u8]`.

---

<div class="post-metadata">

**Author:** ![DarkPhoenix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/darkphoenix/32/47022_2.png) [@DarkPhoenix](https://users.rust-lang.org/u/DarkPhoenix)\
**Post date:** [June 25, 2026, 11:45am UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/10 "2026-06-25T11:45:30Z")

</div>

While original question is a fun theoretical exercise, I took another approach to solving my issue: I just refactored code to use stream-based parsing, so that memory by whole app stays within 30MB in the first place.

As a possible solution, found another person with similar issue (periodical task which spiked memory use to 3-4 GB), he solved it by switching to mimalloc (which costed some performance to him, but the cost was negligibly low).

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 23, 2026, 11:45am UTC](https://users.rust-lang.org/t/allocating-memory-in-a-separate-pool-and-freeing-it/140921/11 "2026-09-23T11:45:34Z")

</div>

Closed on September 23, 2026, 11:45am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
