Memory management

来源:互联网 发布:js将input变成只读 编辑:程序博客网 时间:2024/06/09 23:23

Memory management

 

For memory managementI would like to illuminate it from the classification, memory management, the difference between heap and stack,  and cautions.

v     Memory classification 

From the view of the process memory which is allocated by the operation system, from the low address to high address, that is:

1.       code segment

2.       data segment

3.       run-time heap

4.       shared libraries

5.       user stack,

6.       kernel section

 

v     Memory management

For the programmer’s perspective, the main area of concern is the data area(static memory area), heap, and the stack, The details is below:

1.       Static memory area is auto-allocated by the compiler. They are always alive until the program is end. Such as the static variables and the global variables

2.       The stack memory is also managed by the compiler, The stack memory is used mainly for the local variables and fun parameters. After the function is end, they become invalid.

3.       The heap is used for dynamic memory allocation. it is necessary that the programmer complete the process manually. The C language implements it by the function malloc/free. The C++ language implements it by the operator new/delete.

 

v     The difference between the heap and the stack

1.         In the space, the stack is much smaller than the heap. Generally the stack is measured by MB, whereas the heap is measured by the GB. Actually the main memory is the cashe of the hardware, So in theory, the heap can become very big. But if the program use much memorythat will lead to exchange the data too much. So that it will affect the performance.

2.         In the efficiency, the stack is much higher than the heap. In one hand, The stack is supported by the machine instructions and special register. and the heap is supported by the function. And the algorithm of the function is complex. at the same time ,it is easy that the fragmentation will be made.

v     Cautions

1.         Static memory

1)        Don’t modify the content of the pointer which point to a const string. At the compiling moment, there is no problem, but it will lead to an exception at the running time.

2.         Stack memory

1)        You need to notice the space size of the stack memory

2)        You don’t return a pointer to stack. Because of the address is invalid out of the life circle of the function.

3.         heap memory

1)        After you apply for a block of memory, you must judge if it success.

2)        If you apply for a block of memory successfully, the memory is not initialized, Before you use ityou must initialize it.

3)        After you use, you must release it. Otherwise, it will lead to memory leak ,and that is immoral.

4)        After you release it, you must set the pointer to null,Otherwise it is likely that the others will be damaged by the pointer. Similarly, Don’t manipulate the address that don’t belong to youthat is dangerousit will probably damage the others area.

原创粉丝点击