Google Serach

Loading

Mar 2, 2012

Memory Management

For many programmers, memory management is an important part of the code. Using memory efficiently is not an easy task, and it can be very confusing. However, there are generally two ways in which memory is given to a program: the stack and the heap.
The stack is the simplest one to understand, and is practically ubiquitous in programming. In certain languages, such as the C-based ones, whenever you allocate a variable, for example "int a;" a block of memory from the program's stack is allocated to hold that data. The stack is based on the computer science concept and is a very simple data structure. Whenever your program starts up, it gets a small block of memory. As functions create variables, they get stored in memory on the stack, generally starting at the top and working downwards. This can be done very quickly because many processor architectures such as the x86 architecture have built-in features for the stack. Whenever a function returns, the pointer variable that sets the "top of the stack" simply gets reset to where it was before the function was called. This makes it easy to use memory efficiently. As soon as a function returns, all of its variables are deallocated and the memory space is reused by other functions.
However, this advantage is also a drawback when functions need to return data. Most languages allow a function to return data by placing it on the stack, and having the calling function read it after the function returns. The problem is that this only works on small amounts of data. Imagine, for example, a function that loads a multi-megabyte picture file into memory. The image data can't be placed on the stack because it's generally too small for holding that data. The stack for the entire program is generally only a couple megabytes. If a whole array of pictures needs to be stored, it definitely can't be stored on the stack. An additional program is that memory has to be allocated statically. If an array is placed on the stack, the size of it has to be known at compile time, meaning that stack memory is less flexible.
Heap-based memory allocation solves these problems. Instead of the compiler setting up where variables go on the stack, the program asks the operating system for memory at runtime. In the C language, this is done with malloc(), while in C++, the new keyword accomplishes this. The operating system then returns a pointer to a block of memory that the program can use. The pointer is generally stored on the stack. This method of memory allocation because it solves all of the stack problems. The program can use large amounts of memory, up to whatever the maximum imposed by the operating system is. The memory block doesn't become deallocated when the calling function returns. Instead, it remains available until the program releases the block or the program quits. This solves the earlier problem of a function returning image data. The disadvantage is that the program must be sure to free memory when it's not being used, otherwise memory leaks can occur.
When programming, you need to be aware of your language's memory management system and how it can affect your code. Storing data is a central part of many programs, so not knowing how to properly manage memory can cause seriously problems in the applications that you write.
Thanks for reading my article. There's lots more great information written by me about programming languages at http://programminglanguagehelp.com
Article Source: http://EzineArticles.com/?expert=Bill_Hollins

Article Source: http://EzineArticles.com/6883408

No comments:

Post a Comment