9

Allocate memory on heap is an expensive operation and so some programming languages avoid to give it back to the operating system, even if the allocated memory is not being used anymore.

But for many scenarios, for example microservices running on cloud, you would to like to have low memory usage, otherwise the bill can be quite high. So in these cases it's really important to release the memory after it is not being used.

What is Rust default strategy to uncommit and return memory to the operating system?
How can that be changed?

1 Answer 1

14

By default Rust uses the system allocator.

This is based on malloc on Unix platforms and HeapAlloc on Windows, plus related functions.

Whether calling free() actually makes the memory available to other processes depends on the libc implementation and your operating system, and that question is mostly unrelated to Rust (see the links below). In any case, memory that was freed should be available for future allocations, so long-running processes don't leak memory.

My general experience is that resource consumption of Rust servers is very low.

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

I would also note that someone not satisfied by the default allocator is free to experiment with (1) jemalloc and (2) writing their own GlobalAllocator. Although the latter is, of course, a tad more challenging...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.