In modern computers, memory management also involves managing the virtual addressing system for each running process.
Depending on the programming language, memory management can be manual, automatic, or both.
Contents
What does memory management do?
Every computer has a limited supply of memory that has to be shared across all running processes.
Memory management ensures that this limited resource is freed up when no longer needed.
Many older languages, such as C focus on manual memory management.
Once these variables are no longer required, the developer also has to release the memory again.
For example, if you allocate memory to a variable, a memory address is assigned.
To avoid this, its important to initialise variables when manually managing memory.
More modern languages, such as Python, tend to use automatic memory management.
This automatically runs all the initialisation, and garbage collection procedures in the background.
Using uninitialised memory, for example, can lead to undefined behaviour.
Once a memory location is no longer needed, it should be released.
This allows the computer to reassign it to other software as its needed.
If you dont clean up unnecessary memory after yourself, this is referred to as a memory leak.
Buffer overflows can affect adjacent memory addresses resulting in memory corruption.
Memory management issues result in unintended behaviour.
This primarily takes the form of crashes or errors.
In worst-case scenarios, however, it can result in code execution vulnerabilities.
As such, good memory management is important in developing secure code.
Virtual memory
One of the hidden factors of memory management is the use of virtual memory.
Virtual memory is managed by the operating system rather than the software meaning developers cant really affect it.
Instead of being assigned actual physical memory addresses, each process is assigned its own unique memory address space.
The operating system then converts the virtual address to a physical address whenever it needs to access memory.
One of the key advantages of using virtual memory is that it segments the memory address space between processes.
This prevents one process from being able to read the memory of another.
It also helps to prevent buffer overflows from affecting different processes.
Conclusion
Memory management is the process of managing the limited resource of system RAM.
Mismanaging memory can lead to a large range of memory corruption issues and potentially to code execution vulnerabilities.
The operating system also performs some memory management in the form of virtual addresses.
This allows it to segregate the memory of each process, a useful security feature.
It also allows the operating system to adjust the physical location of data without affecting the actual process.