January 25, 2011

Memory layout in c program?

The distinction between stack and heap relates to programming. When you look at your computer memory, it is organized into three segments:

•text (code) segment
•stack segment
•heap segment

text (code) 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

-The two sections other from the code segment in the memory are used for data.

stack segment
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.

heap segment

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. e.g when you call new() to create new object, the memory allocated from heap. The memory allocated in the heap area is used and reused during program execution. so programmer has to take care about this memory management. e.g when you create new object then when you finished to work with that object you have to call delete(), so system can use this memory for other object.
It should be noted that memory allocated in heap will contain garbage values left over from previous usage. So garbage collection word you will come across when you read about Java programming.

I thought that if a process / executable / binary is being loaded into memory, its organized into these segments:
Data Segment
Text Segment
Stack and Heap Segment

The first two are fairly fixed, Stack and Heap keeps on changing as program executes. Okay there could be multiple stack frames corresponding to call stack. However, there could be only one heap segment per process - where dynamic allocation of memory happens.

So, just was wondering do a process has multiple heap segments inside it.

If there are "multiple" heap segments -
a. What are the min & max number of such segments per process ?
b. Can I control the number of segments Programmatic?
c. If a memory is allocated dynamically for a variable - how do I know which heap segment the variable resides in ?
d. Do I have the option of allocating memory to a particular variable in particular segment ?
e. Say, for example, there are three segments with 2 MB each. I'm trying to allocate 3 MB of memory for a variable. Does the allocated memory spans across segments ?

Heaps suffer from fragmentation. So, OS's try to use various algorithms (filters) to allocated your data. They divide your heap into regions so that you can allocate say 400 bytes of data together, then 800 bytes of data together as so on. If not done so, it may be the case that you don't have enough space in heap to allocate the data. In which case you may think that you would need to move the data in heap. This is not possible (at least efficiently) as you may have stored some pointers to that region in your stack.

With multiple heaps you can avoid this situation. Each heap for a type of data. This is usually true in server applications.

Now, if you have one heap then your data access (say by a thread) is distributed in the process address space. When the memory manager would need to swap a page out (for various reasons) to a page file it may swap a region that your thread would want to use. And when the thread again uses the data, page may have to be brought into memory. If you have multiple heaps, such that your thread accesses only one particular heap (an no other thread), your data are more likely to be bound together (localized) and chances of page swap drastically reduce. In computing I/O (here page swapping) is the key bottleneck to performance.

Heap is a system resource and must be synchronized for thread-safe (and/or multiprocessor) access. With multiple heaps the lock contention reduces.

Plus, with multiple heaps the heap manager would be able to perform actions a lot quicker. Giving you a new/freeing a node.

Multiple heaps do exist. But not in Mars or any other planet - its just inside the * process' address space *. Either you use the Windows API for managing those global, default, dynamic heaps or just use placement new - you're manipulating the same memory allocated to a process - the process' address space - I prefer to call that free space stack and heap - you name it whatever. Can Windows APIs re-size the process' text segment ??!!

You are so wrong. You can't implement placement new on just any part of address space available to you. You would need to reserve and commit the memory. Plus, different heaps are different regions of memory.

1. friends how you are looking at memory ? ie at what level. If you are looking it at a level below your language there are only 2 regions or memory you can access. 1. hardwired Stacks and rest of the memory like cache and cache paged to ram.

2. Heap is a data structure implemented by the language like C n Java I can show language like OCaml or scheme just looking memory as a space of their operation. Coming to stack its a fixed size memory based on the processor architecture. This fixed size is manipulated by each process requesting a fixed size of stack. I know this cos I have written compilers when you translate you program from C to machine code you will also specify the stack size you required.

3. All the run time allocations are placed in the heap(inC) As stack for a program is fixed if you try to copy some dynamic value of bigger size than the stack variable size you will receive Stack smashing attempted error (in modern operating systems eg. Ubuntu 7 onwards Windows XP onwards).

4. For Java every thing is only in Heap at run time. But at the time of byte-code generation at the end of the byte-code you can see Max stack size which is for JVM.

No comments:

Post a Comment