Memory management
来源:互联网 发布:js将input变成只读 编辑:程序博客网 时间:2024/06/09 23:23
Memory management
For memory management,I 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 memory,that 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 it,you 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 you,that is dangerous,it will probably damage the others area.
- Memory Management
- Memory Management
- Memory management
- Memory Management
- Memory management
- Memory Management
- Memory management
- Memory Management
- Memory Management
- memory management
- 8.4 Automatic memory management
- 10.9 Automatic memory management
- c memory management(zz)
- Virtual memory management
- Xen Memory Management
- Inside memory management
- Symbian C++ Memory Management
- Memory Management Functions
- WinCE基础知识(二):WinCE 5.0 的目录结构
- 软件开发一点总结
- [32位汇编系列]003 汇编中__stdcall 调用约定以及参数传递
- FreeBSD7.0+postfix+sasl2+mysql+maildrop+ExtMan+Mailman+ClamAv完全安装
- LINUX下的21个特殊符号
- Memory management
- OSPF详解(二):DR、BDR
- 英语谚语-励志篇
- 链接器中三个最重要的动态链接库kernel32.dll、user32.dll、gdi32.dll的作用
- gdfgd
- 滑板体验(3)
- Redhat9.0环境下Tomcat+Jdk+Mysql的安装
- 看一看黑客的手段 Linux操作系统入侵实例
- PKU JudgeOnline 3401