Freeing in an atexit()

ぐ巨炮叔叔 提交于 2019-11-28 02:56:21

问题


Is there any point to freeing memory in an atexit() function?

I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits anyway?

Is there any benefit to being tidy and actively cleaning it up myself?


回答1:


Not in C - it's like rearranging the deck chairs while the ship sinks around you.

In C++ the answer is different, because objects can delete temporary files and so forth in their destructors, so you need to make sure those get called.




回答2:


One benefit on freeing it is that if you ever do any memory leak testing that tries to match allocations with deallocations over the lifetime of the process you won't get false positives from this kind of deliberate leak.




回答3:


Seeing as malloc()/free() normally involve extensive data structures that exist in userspace, free()ing memory when your program ends can actually be a performance drain. If parts of the data structure are paged to disk, they need to be loaded from disk only to be discarded!

Whereas if you terminate without free()ing, the data paged out to disk can die in peace.

Of course free()ing at other times is usually beneficial as further malloc()s can re-use the space you freed and free() might even unmap some memory which can then be used by other processes.




回答4:


In all modern operating systems, you can safely assume that all memory will be freed when the program exits.




回答5:


Actually being tidy can be interesting when your program evolves.It forces you to write cleanup function when you create "initialisation" functions. The benefit comes when your program becomes more complex, and you want to restart part of the program. If you have already written working cleanup functions, it is less likely that you suddenly forgot some cleanup when "restarting" part of your program.

Writing cleanup functions "lazily" ie only when you need it is more error-prone. Writing cleanup functions forces you to think about cleanup and eventual cleanup dependency. It permit easier code reuse of a part of your code in another project.

So yes freeing in an atexit is useless, and so is closing file descriptor. However writing and maintaining cleanup function as your code grows can be a constraint that will force you to think about what you are doing




回答6:


You should free() if your code that's calling atexit() is part of dynamically-loaded shared library (with dlopen(), for example). In this case the atexit handler will be called at dlclose() time so the heap will continue to exist for the rest of the process to use.




回答7:


On Windows, some calls return memory that belongs to the OS or to COM and you need to free that memory explicitly or it will not be freed even after your process terminates. But this is a rare scenario.




回答8:


not freeing memory before process termination isn't a memory leak. it's a memory leak when you lose a handle to it. but memory is not the only type of resource, and other resources persist across processes (like window handles and file handles), so you do need to 'free' those.



来源:https://stackoverflow.com/questions/231089/freeing-in-an-atexit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!