Manual Memory Management: Must or Mistake?



Manual Memory Management: Must or Mistake?

By Ryan Monaghan


Introduction

In the online discourse among software engineers, the topic of memory management has come up countless times as a source of not only intrigue, but political controversy as well. Memory is a critical piece of a computer, and there are many different ways to optimize it, make it secure, and to manage it. According to Garry Kranz of TechTarget, even implementing a memory cache can speed up a computer “10 to 100 times” when compared to just memory alone. Hardware optimizations aside, in software there are techniques to improve memory usage to improve the speed of programs such as using stack vs heap allocations, garbage collection, and pointers. If you don’t know what any of those things are don’t worry, I’ll explain those and how memory management has become such a central part of the discussion on programming languages in the industry.


Memory Management Techniques

Programming languages that have been around for a very long time, such as C, C++, or Java, all employ most of the techniques that were mentioned previously. Newer low-level languages such as Rust, Go, and Zig choose to implement most of these abstractions differently.

The stack and the heap are two different regions of memory. To put it simply, the stack is best used to store small pieces of information in your program, and it is usually managed automatically by the program. The heap, on the other hand, is where you put large portions of information that you would not be able to store nicely on the stack and is not managed automatically.

Garbage collection aims to solve the manual heap problem by automatically freeing old memory back to your computer, giving you more space to use. Java has a garbage collector which does solve this problem, but the garbage collector can slow down programs that use one, because your computer needs to determine which pieces of information it can discard from memory.

The final item that I mentioned is pointers. This is the most simple to understand concept, but pointers aren’t really a memory management technique. Pointers are the address of a piece of information in memory. Think of your home address. If someone wants to come speak with you, you would give them your address and that person would come visit you. Pointers in code are the very same thing. Some programming languages are pass-by-value which create a copy of a value, and other languages are pass-by-reference which allows the program to use the original value.


Modern Programming Languages

There are many new programming languages since the inception of C and C++, and most of them try to do the same things but better. Rust, the most controversial, does not allow for management of the heap and has no garbage collector, but is extremely secure because of the compiler and how it makes sure that there is no piece of memory that is left unaccounted for. It is also a very fast programming language, on par with the speed that C has, occasionally outperforming C according to Programming Language and Compiler Benchmarks. Zig, a different low level programming language that aims to replace C, allows you to manage the heap by creating your own memory allocator. This allows for more manual control over memory while still making it secure and simple for a programmer to use.


Controversy

Where is the controversy in all this? Well, in 2024, the Biden Administration essentially called for the extinction of C, asking companies and programmers to adopt memory safe languages like Rust, Zig, or Go. The aim of this was to eliminate security vulnerabilities of critical software infrastructure, and to move away from ‘outdated’ languages like C. The paper, which has since been removed by the Trump Administration, states that “in order to reduce memory safety vulnerabilities at scale, creators of software and hardware can better secure the building blocks of cyberspace”, suggesting that old languages should be removed according to The White House. As you can probably infer, engineers were split, saying that this was a nonsensical paper to release, but other advocates for newer programming languages thought that this is a great step forward for software. The main point of discussion was that memory management in certain systems is best to be done manually where there is a limit on how much memory you can use, and that it also makes it easier to write software that performs well.


Conclusion

For the time being, it looks like C and C++ are here to stay. Rust and Zig, while interesting to play around with, are still rather new and do not seem to be ready to be used widely in the industry. I do think that there is a space for these languages in the future, but right now I believe that the current programming languages are secure enough for the modern age.


References

https://www.techtarget.com/searchstorage/answer/What-is-the-difference-between-cache-memory-and-RAM-cache

https://programming-language-benchmarks.vercel.app/c-vs-rust

https://shape-of-code.com/2024/03/03/the-whitehouse-report-on-adopting-memory-safety/

https://upload.wikimedia.org/wikipedia/commons/e/e6/Back_to_the_Building_Blocks_-_A_Path_Toward_Secure_and_Measurable_Software.pdf

Comments

Popular posts from this blog

Molecules, Models, and Magic: The Exciting World of Computational Chemistry

Scaling the Potential of Vertical Farming Going into 2025 and Beyond

Knot Your Average Problem: How do Tongue Ties Impact Oral Myofunctional Health?