Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Rust strategy to uncommit and return memory to the operating system?

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?

like image 806
Leandro Avatar asked Dec 18 '22 12:12

Leandro


1 Answers

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:

  • Does free() unmap the memory of a process?
  • Why does the free() function not return memory to the operating system?
  • Will malloc implementations return free-ed memory back to the system?
like image 193
Sven Marnach Avatar answered Apr 23 '23 09:04

Sven Marnach