Exploring the Benefits of Rust’s Memory Management Approach

Madushan Ranasinghe
The Fresh Writes
Published in
4 min readDec 25, 2023

Rust is a modern statically typed programming language developed by the Mozilla team in 2006. It can be used for various domains including system level programming, web development, network programming and game development. What makes Rust stand out of the rest of the dozen programming languages is it provides benefits such as memory safety, concurrency and zero cost abstraction.

In this article, we will be digging deeper in to rust most distinct feature, which is its memory management. So before going over how rust handle the memory, first we need to look at how memory allocation happens in other popular programming languages.

Manual Memory Management

Manual Memory Management gives complete control of handling memory to the developer, for example C++ provides developers with explicit control over memory operations, allowing them to allocate and deallocate memory as needed. To understand this better lets examine the code snippet below. We can see that num integer is allocated memory at the beginning and deallocated memory at the end manually.

Manual memory allocation and deallocation in C++

Automatic Memory Management (Garbage Collection)

In languages like Python and java, memory handling is done by using a garbage collector. During the garbage collection process, the collector scans heap memory for objects that are no longer in use. If an object no longer has any references to it from the application, the collector removes the object.

Example of garbage collection in java

Drawbacks of these approaches:

So what is the downside with these approaches. While manual memory handling gives full control to the developer, it can lead to memory leaks if developers forget to deallocate memory or if there are errors in the deallocation process. Also, race conditions could occur when you share data concurrently and incorrect usage of pointers could lead to memory leaks.

Garbage collection on the other hand is less error-prone, but you have limited control over allocation of memory. Which could leads to runtime overheads, which might be unacceptable in certain performance-critical scenarios, and it also consumes CPU resources when deciding which memory to free.

How Rust Addresses These Challenges:

Rust takes a different approach when it comes to memory management, which is called the ownership and borrowing concept . Let's look at it.

In Ownership concept, Each value in rust should have an owner, only one can be an owner at a time and after the owner is out of scope the value is removed from the memory.

Let’s take a look at it with the below example so we can get a better understanding

Looking at the above code, the first we create a string called my_string which is mutable (declared using mut keyword). Then we borrow a reference to it in the next line with string_reference. If we try to modify it now it will not compile, this is because we already borrowed the my_string as an immutable.

Let's say we want to create a mutable reference, then we can do it the way string_mut_refernce the variable is declared. Also, when the main function is executed, the value goes out of scope and rust handles the deallocation of memory. So in this way rust’s ownership and borrowing ensure memory safety by preventing multiple parts of the code from simultaneously modifying data.

So, in the end, hope this article was able to show you how rust is different and better at memory management than other languages. Finally, Happy coding, and best of luck with your exploration of Rust.

Thanks for reading.Happy learning 😄

Do support our publication by following it

Also refer to the following articles

--

--

Madushan Ranasinghe
The Fresh Writes

Engineering Undergraduate who loves to explore new technologies.