RSS
Manish.Rathi1

Runtime Memory Management Basics

Sat, Dec 31, 2011

Manish.Rathi1

Memory – One of the most valuable resources of digital world; always remains in high demand just like the Gold in real world. Every active application visible or invisible, certainly require memory for its operations. Due to its nature of limited and high demand, runtime environment has to be very cautious in allocating memory space for program’s objects. Runtime employee various techniques/algorithms in order to fulfil the memory demand of various running applications, considering making all of them mutually happy.  :)

It’s probably hard to imagine and directly going the way these runtime manages, better remain in real world and get conceptual understanding.

Let’s take a simple use case -

Place – Community hall

Capacity – 1000 persons

Objective – Provide the place for each person entering in hall with most convenient way. Convenience can be subjective so let be specific with following–

  • Minimize the allocation time
  • Minimize the disturbance for other existing
  • Smooth capture of empty seats happen due to exit of persons

We have to design a master idea, a virtual program as a solution to above defined use case. Let’s start with simplest one.

Idea 1: Random Allocation.

Person entering can occupy any available location in the community hall. No specific seat allocation rule.

Each time a person enters, he will looks for comfortable place of his choice and occupy. Comfortable location is relative and differs for person to person. Some take front seat, some prefer back, some in center and some might look for seats close to their friends.

As there are no rules Think Time before occupying seat is relatively higher. Each has to think, spend some time his own way.

Idea 2: Sequencing – Force them to sit only next available seat in sequential order.

The moment we apply this rule, Think Time reduced drastically, close to zero. Now places are predetermined, person doesn’t have to spend his time in deciding the place. This idea makes things move faster but it leads into another problem.

Problem - We only considered allocation, and completely ignored the facts of people leaving the hall from any random location. This will create random free seats available, which could be allocated to new person. Our rule does not allow person to sit in empty locations even if they are more preferable/near compare to what next available defined by our rule. Clearly this rule fails to provide the space in least possible time considering empty random locations available.

Idea 3:  Re-arrange

Now to address problem with idea 2 we add another rule, Every time seat freed, we move existing persons, such that vacant locations merged into free location available at the end. Of course we haven’t solved the problem of Idea 2, we just avoided the scenario where problem arising.

At first, things look fine. No empty location remains since every time we move that to end.

Few time later we found ourselves in front of Lion just to avoid barking dogs. We just fall into bigger problem. Doing re-arrangement every time whenever seat gets vacant is overkilling, creating lots of disturbance for the existing mass.

To handle this we have added few sub-idea rules –

  • Do not rearrange every time, do it in interval. Like for every 5th vacant seat.
  • Do this rearrangement in free time, break hours etc.

Doing all these does not gain much advantage and we have to move away from this rule.

We can have hundreds of these rules defined and analysed but it will create blogs virtually impossible to read. I might discover few more rules in next blog, but bigger question to be asked why this all discussion is all about.

Now try to map following entities

Community hall  = Available Memory Area

Capacity 1000 persons  = 1000 memory location, allocation units

Now re-read the Idea’s with mapped memory entities. You will understand the very basics essence of memory allocation.

All platform or programming language has their own strategy for memory management having their own set of benefits and drawbacks. At very broader level they are built around following

– Allocating memory in least possible time with most optimized way
– Freeing memory (Memory Recycling / Reclaiming Free memory)
– Arranging memory (Remove memory fragments)

As we go through community hall use case, though these activities might look simple but there is lot to be discussed. Hope to write more soon …

Popularity: 16% [?]

, , ,

2 Comments For This Post

  1. Bekleidung Says:

    The post was very informative. Thanks for posting it.

  2. Harpreet Kaur Says:

    Interesting Example!!!

Leave a Reply