Memory Management

How your code runs series (Part 1)

ยท

4 min read

Think of the computer memory management this way ๐Ÿค” ...

You just got a new book shelve that has a capacity to take about 200 books comfortably. The information about the capacity of the shelve determines every other thing you do with the shelve. Let's assume that shelve is where you plan to have your everyday books. It means that you have to check it regularly to ensure that only relevant books are in the shelve. You might also be concerned about the size of the books you put in the shelve and also label the books per theme or thereabout for ease of access. As regards the allocation of books also, you may want to put books that you are likely going to reference frequently closer than others. What you are doing is space management. The end point of space management is to be able to utilize a facility (space in this regard) more effeciently. A badly managed shelve conversely would be a headache when trying to find a book.

Technically, what is memory management?

Memory management is the process of controlling and coordinating the computer's main memory. This process ensures optimal allocation of memory space to the operating system, application packages and running processes. An important aspect of memory management is the deallocation of no longer used space or extending such space through the virtual memory. This is crucial as memory management takes into account the capacity limitations of your memory device. Memory management optimizes memory usage so the CPU can efficiently access instructions it needs to execute various processes.

How then does this concern a software engineer?

There are three areas of memory management: hardware, operating system (OS) and program/application.

At hardware level, memory management is concerned with the physical components that store data; specifically the Random Access Memory and the CPU memory caches. Most of the memory management at this level is handled by the Memory Management Unit (MMU) of your PC. The MMU translates logical addresses used by processes to physical addresses on the memory devices. It is integrated into the processor.

At Operating System level, the operating system determines which processes will get memory resources and when those resources will be allocated. One interesting thing the Operating System does is called Swapping. Swapping is a process where the OS temporarily swaps a process out of main memory so the memory is available for other processes. This process is reversed later at an appropriate time.

photo credit: https://www.geeksforgeeks.org/memory-management-in-operating-system/

Photo credit: https://www.geeksforgeeks.org/memory-management-in-operating-system/

At Application/ program level, memory management is handled by the application itself. This is catered for during the application development itself (this is where your role comes into play as a Software Engineer ๐Ÿ˜‰). This aspect of memory management ensures that the program's objects and data structures receives adequate memory. The application memory management is carried out by two related tasks: Allocation and Recycling.

  • Allocation. When the program requests memory for an object or data structure, the memory is allocated to that component until it is explicitly freed up. The allocation process might be manual or automatic. If manual, the developer must explicitly program that allocation into the code. If the process is automatic, a memory manager handles the allocation, using a component called an allocator to assign the necessary memory to the object. The memory manager might be built into the programming language or available as a separate language module.

  • Recycling. When a program no longer needs the memory space that has been allocated to an object or data structure, that memory is released for reassignment. This task can be done manually by the programmer or automatically by the memory manager, a process often called garbage collection.

Low level languages like C, have manual memory management primitives such as malloc() and free(). Conversely, a language like Javascript automatically allocates memory when objects are created and frees this memory when they are no longer needed (note that this doesn't in anyway takes away the responsibility of the developer for memory management).

Does this make sense to you? Kindly repost ๐Ÿ“ญ, like ๐Ÿ‘ and/or comment ๐Ÿ’ฌ.
Cheers!

ย