Pages

Thursday, December 23, 2010

Memory Management - Static Memory Management

In C or C++, you can assign memory to a variable or an object in two ways,
1. Static Allocation
2. Dynamic Allocation

which method will be used to allocate memory, depends on whether the data is fixed or varying. If the amount of data is fixed the static allocation method should be used and in case of varying data, the dynamic allocation method is more suitable.


Static Memory Allocation
As you know a single variable can hold only a single value, whereas an array can store multiple values. For example, if you wish to store salary of an employee, you can simply create a variable and store the salary there. However, if the number of employees is quite large, say 50 or 100, you can create an array of the same size and store the data. The array will save you from the trouble of declaring so many variables. But  whatif the number of employees is not known? Even an array will not help you . Why, because the arrays are static in nature. Once you declare an array, you can not be modify while the program is running or what is known as the "run time". So if you are asked to store the salaries of "some" employee how large an array would you create? If you say 50, and the actual number of employees is just 5 then? In this case the remaining memory will be wasted. If a smaller size is decided upon, it will lead to loss of data. To sum it up, following are the disadvantages of an
array.
1. You can not insert an element in an array during the run time.
2. You can not delete an element from an array during the run time.

Therefore arrays are recommended only when amount of memory is known beforehand, otherwise you will either lose memory or data.
The core of the problem is the way the memory is being allocated - Statically. The moment you write a statement like "int a", 2 bytes of memory are set aside for the variable "a". This memory will be allotted to the variable whether or not you initialize the variable. This poses a serious problem - suppose you have a float array of 100 elements(100 x 4 = 400 bytes), which is not needed after the first 10 lines of the program. From 11th line onwards will the array still occupy 400 bytes or not? The answer is Yes.t The memory which is assigned to a variable statically cannot be deallocated even if the user wishes. Therefore if you keep allocating memory to variables, chances are you may run out of memory.

Is wastage of memory such an issue?
Some people would say, "I need not worry about memory, I have 4 GB RAM in my system. So even if 1 or 2 MB goes waste, it is not a big problem". They are wrong, it is a big problem for the reasons given below.
1. You are not making the program for yourself. The program is made for the user or the client. You may be having 3 to 4 GB of RAM in your system, but what if the user is still using a decade old system which  has 64 or 128 MB RAM. It will be a costly affair for such a user if some memory is wasted by the program. Asking the user to upgrade the same is not always recommended.

2. For those who have sufficient amount of RAM, they need to keep in mind that even they can't afford to waste it. Here's why - Let's take an example of a system which has 1 GB of RAM. The moment the system is switched on, a small part of this memory is spent in displaying graphics on the monitor which is called the shared memory. When the initial process completes, the OS (Windows, Linux or any other)  starts to load into the memory which is called "booting". OS consumes a lot of memory to load, specially the new ones like Windows Vista or Windows 7. Once the system is up and running the user starts opening applications which he wants to use, like a media player,word,excel or any other program. Each program or the file the user opens gets loaded in RAM, thereby decreasing it. When the user starts to write a program the compiler will not the count the memory as 1 GB. The amount of memory will be calculated in term of "free memory". which means the amount of physical RAM(1 GB) minus the amount of memory used by the OS and all the other programs. All of the free memory will not be given to the user to declare variables since the system will  need some memory for its internal processes also. Therefore it allots the program a very small amount of the available RAM called the free store or the
heap memory fulfil its needs. The size of the free store is relative to the amount of physical RAM, higher the RAM, more the free store. In a nutshell, the total RAM in your computer is not given to your program to process, just a small amount. To understand it, suppose the free store is of 1 MB and the program has 100 integer variables, 50 floats and 200 char variables, the program will demand a total of(100*2,50*4,200*1) 600 bytes from 1 MB. Don't forget that even a small "Student Management Program" will be needing more than 1 MB of RAM to declare the variables.
Dynamic Allocation is the answer to all the problems. If the memory is allocated dynamically it can be deallocated any time keeping the free store always ready with bytes of memory for future use.

0 comments:

Post a Comment