Friday, January 6, 2012

stack vs heap

how the memory of the computer is organized for a running program
When a program is loaded into memory, it is organized into three areas of memory, called segments:
the text segment,
stack segment,
heap segment.

The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides
This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The stack is where memory is allocated for automatic variables within functions.

The heap segment provides more stable storage of data for a program;
memory allocated in the heap remains in existence for the duration of a program.
Therefore, global variables (storage class external), and static variables are allocated on the heap.


The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored
The heap is the section of computer memory where all the variables created or initialized at runtime are stored

computer memory, it is organized into three segments:

text (code) segment
stack segment
heap segment


The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions.


Heap and stack in Java
When you create an object using the new operator, for example myobj = new Object();, it allocates memory for the myobj object on the heap.
The stack memory space is used when you declare automatic variables.

for example String myString;, it is a reference to an object so it will be created using new and hence it will be placed on the heap

Reference:
http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html
http://www.maxi-pedia.com/what+is+heap+and+stack





  • Stack memory

-Size required determined at compile-time, so no need at run time to determine amount of memory to allocate at run time. This is obviously a performance advantage
In contrast, the size of objects allocated on the stack, needs to be known at compile time

-Automatic "clean-up"
your program takes care of stack memory (allocating it & cleaning it up)

-The most important thing about the stack is that you don't need to think much about it. Your program manages it.

-The size of the stack is usually quite small because it's mostly used for local variables (variables that go out of scope pretty soon after they're finished with). The size of the stack is set at compile time and is small enough to be available on any computer, no matter how little RAM it's got

-Stack is where variables declared before runtime are stored.





  • Heap memory

-It's also used very frequently to allocate memory for things whose size is not known until run time. For example, if you had to open a file and read in the data, you don't know how much data exists until run time

-The size of objects allocated on the heap is often not known to the compiler - therefore the programmer must allocate and release the memory specifically.

-If a particular computer has a lot of RAM, why not make use of it?
Therefore the heap consists of memory that can be allocated at run time

-Heap memory is most often used for 'large' things (e.g. the contents of a file) - or for things that need to 'stay around' in memory so that different functions can share the data (in other words, for NON-local variables).

-Heap is where variables created or initialized at runtime are stored.

-On the other hand, heap is an area of memory used for dynamic memory allocation


http://hep.ph.liv.ac.uk/~gwilliam/cppcourse/




  • When you look at your computer memory, it is organized into three segments:

?text (code) segment
?stack segment
?heap segment



The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions. This includes all user defined as well as system functions.




  • What is stack?

The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.

Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.






  • What is heap?

On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The stack is much faster than the heap but also smaller and more expensive.









No comments:

Post a Comment