Dynamic Memory management in Rust

I would like to know how dynamic memory is manged internally in Rust.
If some one could point me to the article or explain, that will be nice.

Is it like ptmalloc2 in Glibc ?. I'm looking for something like that.


I think you'll find the global allocators - RFC 1974 and its predecessor swap out jemalloc - RFC 1183 interesting reads. The latter contains a link to a blog post by Niko Matsakis from 2014: Allocators in Rust. There's also a section in the nomicon on allocation (showing how you can grow a vector using Rust's allocation APIs) that you may find helpful. Otherwise you can find Rust's allocation APIs in the std::alloc module.


tl;dr: Rust by default uses the malloc/free provided by libc on Unix, HeapAlloc/HeapFree on Windows and equivalent mechanisms on other OSes where possible. Only if a target doesn't have an allocator of it's own does Rust bring it's own allocator as default allocator and in that case it is generally an allocator optimized for code size rather than performance. In all cases you can override the allocator using #[global_allocator].


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.