了解内存泄漏 [英] Understanding Memory Leaks

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

问题描述

假设我有一个main()函数,其中只有一个malloc语句分配了大约1 gb的内存.还说我的系统有1 GB的内存.

Suppose I have a main() function with only one malloc statement allocating say some 1 gb memory. Also say my system has 1 gb of ram.

main()
{
    malloc(1gb)

    return(0)
}



上面的程序退出而没有释放内存.

在这种情况下,是否会在上述进程终止时将1 gb的堆内存返回给系统,或者堆内存永久泄漏,并且仅在系统重新启动时可用?

我的意思是,如果说我们开始另一个进程或再次说相同的进程,那么如果再次进行堆,他们将能够获得1 GB的内存吗?



The program above exits without freeing the memory.

In this case will the 1 gb of heap memory be returned to system on above process termination or has the heap memory permanently leaked and will be available only on system reboot?

I mean if say we start some another process or say the same process again then will they be able to get the 1 gb if heap again?

推荐答案

所有现代操作系统将在应用程序结束时回收所有堆内存,但嵌入式系统可能不会回收-取决于它们的写入方式.
All modern operating systems will reclaim all heap memory when the application ends, but embedded systems may not - it depends on how they are written.


内存将返回到空闲内存池(尽管这样做会可以使一个没有的操作系​​统成为可能,但是我确定Windows,Linux和OSX可以足够智能地管理它们的内存以对其进行回收),因为它已分配给该进程,并且当该进程终止时,该OS可以回收所有内存.应用程序使用的资源.

如果启动相同的进程,则必须再次分配它,但是假设尚未将内存分配给另一个进程,那么这样做就不成问题了.不过,不太可能再次获得完全相同的内存,因为OS会不断根据进程的请求向进程分配内存.

对于另一个过程,这取决于.如果内存是共享的,那么它将不会被回收,因为其他进程仍在使用它.如果您指的是之后启动另一个进程,那么它可以在OS空闲的情况下(由上面显示的应用程序退出)由OS为其分配该内存.

我认为有一种分配内存的方法(或者曾经可能不再可行),而该内存将无法在Windows中被操作系统回收,从而导致内存泄漏,直到重新启动为止,但我不记得它是什么.至于其他操作系统,我不确定,每个操作系统都以自己的方式管理内存.
The memory will be returned to the free memory pool (although, it would be possible to make an OS that did not, but I''m sure Windows, Linux, and OSX manage their memory intelligently enough to reclaim it), because it is given to that process, and when the process is terminated the OS reclaims all resources used by the application.

If you start the same process, it will have to be allocated again, but assuming the memory hasn''t been allocated to another process it shouldn''t be an issue to do so. It''s unlikely you will get the exact same memory again though, because the OS is constantly allocating memory to processes as they request it.

For another process, it depends. If the memory was shared, then it won''t be reclaimed, because the other process is still using it. If you mean another process launching afterwards, then it can have that memory allocated to it by the OS if it''s free (and it will be if the application shown above has exited).

I think there is (or was, it may no longer be possible) a way to allocate memory that the OS would not reclaim in Windows, leading to a memory leak until reboot, but I don''t remember what it was. As for other OS''s, I''m not sure, each manages memory in their own way.


不,操作系统很聪明,当进程终止时它们会进行大量清理(释放内存,文件句柄等)
一些程序员甚至依赖于此.
Nope, OS are smart, they do a lot of cleanup when a process terminates (releases memory, file handles, etc.)
Some programmers even relies on that.


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

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