什么是内存泄漏? [英] What is a memory leak?

查看:61
本文介绍了什么是内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很明显维基百科关于该主题的信息很多,但我想确保我明白.从我能说出的意义上,了解堆栈/堆的关系对真正了解内存泄漏很重要?

Obviously Wikipedia has a fair amount of information on the topic, but I wanted to make sure I understand. And from what I can tell it's important to understand the stack/heap relationship to really understand a memory leak?

这就是我(认为)我所理解的.更正欢迎您!

So here's what I (think) I understand. Corrections are very welcome!

首次启动程序时,会分配一个内存块,例如0x000至0xFFF.第一部分(例如0x000至0x011)是加载程序代码的代码/文本段.

When you first start your program, a block of memory is allocated, say 0x000 to 0xFFF. The first part (say 0x000 to 0x011) is the code/text segment where the program code is loaded.

+--------------+ 0x011
| Program Code |
+--------------+ 0x000

然后,您拥有用于保存局部变量的堆栈(例如0x012至0x7ff),并且将它们存储/检索到FIFO.因此,如果您有类似

Then you have the stack (say 0x012 to 0x7ff) that holds local variables, and they are stored/retrieved FIFO. So if you had something like

char middleLetter(string word){
     int len = word.length();
     return word[len/2];
}

int main(){
   int cool_number;
   char letter;
   letter = middleLetter("Words");
   ...

然后,您的变量将在堆栈中分配,如下所示:

Then your variables would be allocated on the stack, which would look like this:

+-------------+ 0x7ff
|             |
|             |
|             |
| ...         |
| len         |
| letter      |
| cool_number |
+-------------+ 0x012

当然,如果您正在某个地方分配内存(使用mallocnew),但从未释放它,那么您的堆可能看起来像这样,并且现在您遇到了内存泄漏:

Of course, if you were allocating memory somewhere (using malloc or new), but never freeing it, then your heap could look like this, and you now have a memory leak:

+-------------+ 0xfff
|             |
| malloc(20)  | 0xf64
| malloc(50)  | 0xf32
| malloc(50)  | 0xf00
| ...         |
|             |
+-------------+ 0x800

这意味着,尽管您可以使用指针算法直接访问0xf32,但OS/您的程序认为内存位置0xf00-0xf46已被占用,并且除非您的程序被使用,否则再也不会使用这些位置进行存储.关闭并释放内存.但是共享内存呢?维基百科说它将永远不会被释放(直到您的计算机重新启动?).你怎么知道它是否是共享内存?

What this means is that while you can directly access 0xf32 with pointer arithmetic, the OS/your program thinks that memory locations 0xf00-0xf46 are already taken, and won't ever use those spots for storage again, until your program is closed and the memory is freed. But what about shared memory? Wikipedia says it won't ever be released (until your computer is restarted?). How do you know if it's shared memory?

这是一个很好的基本理解吗?我有什么想念的吗?感谢您的光临!

Is this a pretty good basic understanding? Is there anything I'm missing/have wrong? Thanks for looking!

推荐答案

似乎您确实理解它-一个例外:在您的示例中,len是一个与其他变量一样的堆栈变量. newmalloc在堆上创建,其他所有内容(局部变量等)都在堆栈上.而且main的局部变量与其他函数的变量没有什么不同.

Seems like you do understand it - with one exception: In your example, len is a stack variable like everything else. new or malloc create on the heap, everything else (local variables etc) is on the stack. And main's local variables are not different from any other function's variables.

共享内存是一种非常罕见的情况,通常不需要共享内存,因此除非您明确要求共享内存,否则共享内存将不存在(否则,某些随机的其他进程可能会使用与您的进程使用的内存相同的内存-显然,这会严重破坏一切.)

Shared memory is a rather rare case, you usually don't need it and therefore you won't have it unless you explicitly ask for it (otherwise, some random other process may use the very same memory your process uses - obviously, this would break things badly).

这篇关于什么是内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆