如果在程序终止时操作系统将回收内存,为什么要释放内存 [英] Why should we free the memory if it will be reclaimed by the OS at program termination

查看:116
本文介绍了如果在程序终止时操作系统将回收内存,为什么要释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题或更确切地说是好奇心..



在以下程序中,我是否需要释放分配的内存?







I have a question or rather a curiosity to know..

In following program, Do i need to free the allocated memory ??



int main()
{
  char * cpTest = new char[32];

  ...
  ...

  
}





即使我没有释放它,它也会被操作系统收回。那我为什么要关心这个呢?



Even if i do not free it , it will be reclaimed by the OS. Then why should i care about this ??

推荐答案

因为这是一个很好的做法。

这意味着这是个好主意养成释放你分配的一切的习惯。如果你每次都这样做,确保当你分配一些东西时,你会自动设置代码来解除分配,那么它就变成了第二天性,当yoiu正在处理更大的项目时,你不会忘记。



如果您认为这次没关系,然后将代码移到一个函数中,那么您是否会添加解除代码?不 - 因为你不需要它。或者甚至考虑一下。没有任何事情会导致问题。



所以当你在3月份想要一个函数在不同的项目中做同样的事情时,你会记住这个函数并复制'' n''patse ....宾果!它工作正常!



然后在八月份,当你的项目变得庞大并且你有100个用户时,他们开始尖叫你,因为你的应用程序内存不足和崩溃当他们使用它时,你会记得那个记忆没有被释放吗?或者必须经常搜索大量代码才能解决这个问题?



立即行动吧。永远这样做。不要依赖操作系统来清理你。
Because it is good practice.
Which means it''s a good idea to get into the habit of freeing everything you allocate. If you do it every time, making sure that when you allocate something, you automatically set up the code to de-allocate it, then it becomes second nature and you don''t foget when yoiu are working on larger projects.

If you think "it doesn''t matter this time" and then later move the code into a function, will you add teh de-allocate code then? No - because you don''t need it. Or even think about it. And nothing happens that causes a problem.

So when in March you want a function to do the same thing in a different project, you remember this function and copy''n''patse....bingo! it works!

Then in August, when you project has got huge and you have 100 users, and they start screaming at you because your app runs out of memory and crashes while they use it, will you remember then that the memory wasn''t deallocated? Or have to search tediously though reams of code trying to work it out?

Do it now. Do it always. Never rely on the OS to clean up after you.


在这种特殊情况下,没有令人信服的理由去关心,但这是一个非常好的习惯,可以让你自由一切在适当的时候使用 RAII 进行分配,因为99.99%的时间没有这样做将是一个不受控制的内存泄漏,潜在的安全风险,任何工具中的巨大噪音,如 valgrind 您正在使用它来追踪错误,而且通常是糟糕的做法。如果你是一个经验丰富的开发人员,他的东西不会泄漏,因为他总是清理,然后通过一切方式修剪你的清理代码的边缘,而不是打扰最后几个全球对象,你将是在良好的公司中,微软自己的C运行时也会这样做。
In this particular case there''s no compelling reason to care but it''s an extremely good habit to get into to free everything you allocate, using RAII where ever appropriate because 99.99% of the time not doing so will be an uncontrolled memory leak, a potential security risk, massive noise in any tool like valgrind that you''re using to track down a bug and just generally bad practice. If you''re an experienced developer who''s stuff doesn''t leak because he always cleans up then by all means trim the edges of your winding up code by not bothering with those last few global objects, you''ll be in good company, Microsoft''s own C runtime does the same.


你好,



解放程序中的对象'生命不是强制性的,但建议。

确实,操作系统将回收程序使用的内存,它会终止,但是如果你的程序在执行时会分配大量的内存块导致一些NotEnoughMemoryError并且如果你不处理那种错误可能会让你编程。



释放内存的目的不是为了回忆内存到系统但首先到你自己的程序。内存分配在程序的虚拟内存中完成(如果使用x86目标编译,最高可达4G,如果在x64目标上编译,则最高可达系统限制),当程序耗尽虚拟内存时,无法分配对象再过了。



现在关于你发布的代码片段我会说你丢弃之后不需要释放那个记忆困难...但仍然是你的goor练习,如果你要针对一个实时系统或一个受限制的系统(比如一个嵌入式微系统)进行开发,你将不得不在你不再需要它时立即释放它。



希望这对你有意义。
Hello,

Freeing objects along a program''s life is not mandatory but recommended.
It is true that the OS will reclaim the memory used by your program whem it will terminate, however if your program allocates a lot of memory blocks while executing that could cause some "NotEnoughMemoryError" and might take you program down if you don''t handle that kind of error.

The aim of freeing memory is not to give back memory to the system but first to your own program. Memory allocations are done in your program''s virtual memory (up to 4G if compiled with x86 target, up to the system limit if compiled on x64 target), when your program runs out of virtual memory, it can''t allocate objects anymore.

Now regarding the code snippet you posted I''d say you don''t really need to free that memory straigth after discarding... but still that''s goor practice and were you to develop against a real time system or a constrained system (say an embedded microsystem as an example) you would be obliged to free it as soon as you don''t need it anymore.

Hope this makes sense to you.


这篇关于如果在程序终止时操作系统将回收内存,为什么要释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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