Day 14 — CS Fundamentals December — About Operating Systems — Memory Management-1

Yashvardhan Kukreja
5 min readDec 15, 2019

--

See, computers love to have more amount of primary memory/RAM coz why not. But harsh reality is that there is not as much RAM possible to accommodate as hard disk, if we speak in terms of cost.

So, managing the processes the right way so that RAM always occupied and gets perfectly utilized helps in reaping out maximum amount of memory from RAM.

So, if you think like a computer, then Memory Management, indeed, is cool.

So, I am going to be writing two articles on this topic (including this one).

  • Today, this article is going to be about Introduction to Memory Management and a problem called Fragmentation
  • Tomorrow, the next article is going to be about Paging and Segmentation (you’ll get to know that later, chill!)

Let’s dive in!

So, as I explained above why memory management is cool and crucial for CPU.

So, RAM is basically subdivided into memory blocks. And the whole idea of memory management revolves around ways to add processes/programs from disk to the RAM’s memory blocks in such a way that maximum number of memory blocks are occupied.

By the way, you never get all the memory blocks of the RAM. That’s because whenever the Operating System boots up and already occupies some top memory blocks of the RAM for its own functioning.

But there are different numerous ways to perform memory management depending on the type and number of processes we are dealing with.

Broadly, speaking memory management techniques are divided into two categories: contiguous and non-contiguous

Contiguous Memory Allocation

Here, we allocate the processes in contiguous memory blocks in the RAM.

For example, a process, divided into 3 segments P1, P2, P3 might occupy memory blocks in the RAM like block 1 (-> P1), block 2(-> P2), block 3(-> P3) ….. OR …. block 5 (-> P1), block 6 (-> P2), block 7 (-> P3)……. BUT NEVER ….. block 1 (-> P1), block 3 (-> P2), block 7 (-> P3)

There are two further types of contiguous memory allocation techniques:

  • Fixed Partition or Static: Here, the RAM is already divided into memory blocks of fixed size, say, 512KB. So, say, a process comes in requiring 1MB of RAM. Then, it will be allocated to fixed sized contiguous blocks, say, block 2 and block 3(512KB + 512KB = 1MB).
  • Variable Partition or Dynamic: Here, the space to process is totally provided at the runtime on the basis of its space requirements. For example, a process comes in requiring 1MB of RAM. Then, here RAM will give it a dynamically created memory block of 1MB.

Incontiguous Memory Allocation

As the name suggests, memory blocks allocated to the segments of the process will be incontiguous.

For example, a process having 4 segments, say, P1, P2, P3, P4 will be allocated incontiguous memory blocks in the RAM like block 1 (-> P1), block 3 (-> P2), block 7 (-> P3), block 13 (-> P4)

There are multiple types of incontiguous memory allocation techniques:

  • Paging
  • Segmentation
  • Multilevel Paging
  • Segmented Paging

I will be discussing about them in detail in tomorrow’s article because they are pretty detailed.

But wait, there’s a problem

So, memory management involves loading processes into RAM and eventually, unloading them.

Now, as processes are loaded and unloaded from memory, the free memory space is broken down into little pieces. When this happens a lot, all we are left with are many tiny memory blocks.

Now, technically, we have available memory blocks but the individual memory blocks are so small and tiny that most of them can’t even accomodate a single segment of a process hence, making it impossible to load the process into RAM despite of the so-called available RAM.

This problem is known as Fragmentation.

There are two types of Fragmentation:

  • External Fragmentation: Although the total available RAM is enough to satisfy the request of the process, but it is not contiguous, hence, it cannot be used.
  • Internal Fragmentation: Memory block assigned to the process is bigger and the remaining free space in the memory block is left unused and can’t be used by another process.

“This seems serious, Yash! How the heck do we deal with this? :/”

So,

  • External Fragmentation can be reduced by a process called compaction where, you shuffle memory contents in such a way that the distributed tiny empty memory blocks are brought together after shuffling and then, they are coalesced to form a large empty memory block which can accommodate a process.
  • Internal Fragmentation can be reduced or “prevented” by effectively assigning the smallest memory block to the process but it should be jussttt large enough to accommodate that process.
Thanks to tutorialspoint.com for this image :)

That’s it!

Thanks for reaching till here :)

I know this was a short article but memory management is a topic which will take two articles and the best way which seemed to divide the articles is the way I am doing right now :P

Anyways, I hope you understood everything in this article and if you liked it, do give it some claps :)

Stay tuned for tomorrow’s article where I am going to be writing about Paging and Segmentation incontiguous memory allocation techniques.

Find me on

LinkedInhttps://www.linkedin.com/in/yashvardhan-kukreja-607b24142/

GitHubhttps://www.github.com/yashvardhan-kukreja

Email — yash.kukreja.98@gmail.com

Adios!

--

--

Yashvardhan Kukreja

Software Engineer @ Red Hat | Masters @ University of Waterloo | Contributing to Openshift Backend and cloud-native OSS