简体   繁体   English

我的程序调试期间是否可能导致内存泄漏?

[英]Can I cause memory leaks during debug of my program?

I'm developing on Ubuntu 9.10 我正在开发Ubuntu 9.10

I'm writing a C program, during my tests & debugs I'm calling malloc and always remember to call free() - This is obviously just during debug. 我正在编写一个C程序,在我的测试和调试期间,我正在调用malloc并且总是记得调用free() - 这显然只是在调试期间。

I'm curious: Am I eating up the free memory the system has which each debugging session? 我很好奇:我是否正在吃掉每个调试会话系统所具有的空闲内存? Or does the kernel cleans up the process memory after I shutdown my application from the IDE? 或者,在我从IDE关闭应用程序后,内核是否清理了进程内存? Logically reasoning I'm pretty sure that the kernel knows about the whole process being killed and thus knows what memory it has allocated, and thus even though the application did not call free the memory is still being released. 逻辑推理我很确定内核知道整个进程被杀死因此知道它分配了什么内存,因此即使应用程序没有调用free,内存仍然被释放。

I would appreciate an explanation. 我很感激解释。

Thank you, Maxim. 谢谢你,马克西姆。

是的,操作系统将在程序停止运行时回收分配给程序的所有内存。

You are correct. 你是对的。

This is not a serious concern, because any 'leaked' memory is immediately freed once the program you're debugging is terminated. 这不是一个严重的问题,因为一旦你正在调试的程序被终止,任何'泄露'的内存会被立即释放。

Memory leaks are generally only a concern when a process is long running. 当进程长时间运行时,内存泄漏通常只是一个问题。

The kernel has a collection of process records within the kernel's memory and keeps track of each process, the amount of memory consumed, resources such as i/o, file handles or inodes. 内核在内核的内存中有一组进程记录,并跟踪每个进程,消耗的内存量,诸如i / o,文件句柄或inode等资源。 The process records are usually held in a queue in which the kernel's task pointer points to the process record in a never ending fashion (that explains why the perception of 'multi-tasking', it is doing that a blink of an eye - so fast, really, it is doing single tasking in the eyes of the kernel). 进程记录通常保存在一个队列中,其中内核的任务指针以永无止境的方式指向进程记录(这解释了为什么“多任务”的感知,它正在眨眼之间 - 这么快,实际上,它是在内核眼中进行单任务)。 There is a field in the process record that tells how much memory is chewed up by said process. 进程记录中有一个字段,用于说明所述进程占用了多少内存。

Yes the kernel does obtain the memory back to its own pool ready for consumption by another process. 是的,内核确实将内存恢复到自己的池中,以备另一个进程使用。 Furthermore, you are absolutely 100% correct in relation to memory leaks as John Weldon above pointed out. 此外,正如John Weldon上面指出的那样,对于内存泄漏,你绝对是100%正确的。 I mentioned this in another posting , for every malloc there is a free, if there isn't you have a memory leak. 我在另一个帖子中提到了这一点,因为每个malloc都有一个免费的,如果你没有内存泄漏的话。 So don't worry about your debugging session. 所以不要担心你的调试会话。 That is perfectly ok as the kernel has a responsibility to ensure the memory is reclaimed. 这完全没问题,因为内核有责任确保内存被回收。

Some applications (especially daemons) must be debugged throughly and that no memory leaks occur in it as the daemon will be running for a long time before the next reboot. 某些应用程序(尤其是守护程序)必须彻底调试,并且不会发生内存泄漏,因为守护程序将在下次重新引导之前运行很长时间。 Incidentally, it was mentioned in my favourite book 'Expert C Programming, Deep C Secrets - Peter Van Der Linden', that at one stage when he was in Sun, there was a tool called 'printtool' for printing, but every so often the queue jammed up because there was a memory leak in the print spooler program and the reboot of the Sun machine cured it, he describes that in detail in relation to memory leaks. 顺便提一下,在我最喜欢的书“专家C编程,深度C秘密 - 彼得范德林登”中提到过,在他进入Sun的一个阶段,有一个名为“printtool”的工具用于打印,但每隔一段时间队列堵塞,因为打印后台处理程序中存在内存泄漏,并且Sun机器的重新启动使其固化,他详细描述了与内存泄漏有关的问题。

Hope this helps, Best regards, Tom. 希望这会有所帮助,最好的问候,汤姆。

A lot of old Unix apps leaked memory badly, and counted on the end-of-process cleanup. 许多旧的Unix应用程序严重泄露内存,并依赖于进程结束清理。 Even in the limited address space of those days, it generally worked reasonably well. 即使在那些日子的有限地址空间中,它通常也能很好地工作。 This doesn't work with long-running apps, of course. 当然,这不适用于长时间运行的应用程序。 I wouldn't worry about the effects of memory leaks when debugging (the leaks themselves are bugs, so you'll want to remove them before releasing). 调试时我不担心内存泄漏的影响(泄漏本身就是bug,因此你需要在发布之前删除它们)。

What happens in Unix, and all other current OSs that I'm actually familiar with, is that the OS assigns memory to a process. 在Unix中以及我实际熟悉的所有其他当前操作系统中发生的事情是操作系统为进程分配内存。 malloc() grabs memory out of the process memory, and if it asks for more than the process can provide will ask for more process memory from the OS. malloc()从进程内存中获取内存,如果它要求超过进程可以提供的内容,则会从操作系统请求更多的进程内存。 If the program's got a memory leak, the process memory can grow as large as the system will allow, but it's all process memory, and all the allocation will go away when the process ends. 如果程序有内存泄漏,那么进程内存可以增长到系统允许的大小,但它是所有进程内存,并且当进程结束时所有分配都会消失。

I understand there have been OSs that didn't recover memory from a terminated process, but I haven't seen one or worked on one. 我知道有些操作系统没有从已终止的进程中恢复内存,但我还没有看到一个或在一个上工作。 The only OSs for personal computers (as opposed to special purpose or enterprise computers) that significant numbers of people use are Windows and Unix variants, and those will release all memory at the end of the process. 个人计算机(相对于特殊目的或企业计算机)的唯一操作系统是大量人们使用的Windows和Unix变体,并且这些操作系统将在流程结束时释放所有内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM