问题
Possible Duplicate:
When you exit a C application, is the malloc-ed memory automatically freed?
This question came to my mind when i was reading about how compulsory it is to use delete/free respectively when it comes to dynamic memory allocation in C/C++. I thought if the memory allocation persisted beyond the termination of my program execution, then yes it is compulsory; otherwise, why do i have to worry about freeing up the allocated space? Isn't the OS going to free it up automatically with process termination? How right am i? My question is that can
int *ip = new int(8);
persist beyond the termination of my program?
回答1:
Short answer: No.
Long answer: No. C++ will never persist memory unless you do the work to make it do so. The reason to free memory is this:
If you don't free memory, but keep allocating it, you will run out at some point. Once you run out, almost anything can happen. On Linux, maybe the OOM killer is activated and your process is killed. Maybe the OS pages you completely to disk. Maybe you give a Windows box a blue screen if you use enough memory. It can almost be thought of as undefined behavior. Also, if you leak memory, it's just siting there, unused, unreleased, and no one can use it until your process terminates.
There's another reason too. When you release memory to the allocator, the allocator might keep it around, but just mark it as usable. That means that next time you need memory, it's already sitting there waiting for you. That means there are less calls into the kernel to ask for memory, increasing performance, as context switches are very inefficient.
EDIT: The C and C++ standards don't even give a guarantee that the memory will be cleaned up by the OS after termination. Many OSes and compilers may, but there is no guarantee. Despite this, all major desktop and mobile operation systems (with the exception of probably DOS and some very old embedded systems) do clean up a processes memory after it.
回答2:
You do not need to release the memory back to the OS before the program exits, because the operating system will reclaim all memory that has been allocated to your process upon the termination of the process. If you allocate an object that you need up to the completion of your process, you don't have to release it.
With that said, it is still a good idea to release the memory anyway: if your program uses dynamic memory a lot, you will almost certainly need to run a memory profiler to check for memory leaks. The profiler will tell you about the blocks that you did not free at the end, and you will need to remember to ignore them. It is a lot better to keep the number of leaks at zero, for the same reason that it is good to eliminate 100% of your compiler's warnings.
回答3:
Any good OS should clean up all resources when the process exits; the 'always free what you allocated' principle is good for two things:
If your program leaks memory but never exits (daemons, servers, ...) continuously leaking memory will waste RAM badly.
You should not defer freeing all memory until your program terminates (like Firefox does sometimes - noticed how much time it takes for it to exit?) - the point is to minimalize the time for which you have allocated memory; even if your program continues to run, you should immediately free up allocated RAM after you are finished with it.
回答4:
1) Free your memory when you request if off the heap. Memory leaks are never a good thing. If it doesn't hurt you now, it likely will down the road.
2) There is no guarantee by C or C++ that your OS will clean up the memory for you. You may be programming some day on a system that, in fact, does not. Or worse, you may be porting code in which you didn't care about memory leaks to this new platform.
回答5:
For a historical note: the operating system used by old Amiga computers (“AmigaOS”) did not have full memory management as it is assumed now (except maybe for some later versions released when Amiga was no longer popular).
The CPU did not have a MMU (memory management unit), and as a consequence every process had access to all physical memory. Because of that when two processes wanted to share some information, they could just exchange pointers. This practice was even encouraged by the OS, which used this technique in its message-passing scheme.
However, this made it impossible to track which process owns which part of memory. Because of that the OS did not free memory of a finished process (or any other resource, for the fact). Freeing all allocated memory was therefore vital.
回答6:
If you are very sure that you will never need to free the memory in the lifetime of the program, technically it may be alright to skip free/delete. Operating systems like Linux, Windows etc. will free up the allocated memory when the process ends. But in practice you can almost never make the assumption that the memory you allocate does not need to be freed within the lifetime of the process. Keeping code reusability, maintainability and extensibility in mind, it is a good practice to always free up everything that you allocate at the appropriate place.
回答7:
This is an interesting question. My original take on your question was whether or not you could access memory after program completion, but after a second read I see you want to know why memory should be freed.
You free dynamically allocated memory, because if you don't the OS and other process will run out and you will have to reboot.
I thought you might want to access that memory after program completion, so my guess is that even if you wrote out the starting address and length of a dynamically allocated memory block -- to the console or a file -- that address might not be valid after the program completion.
That is because when your program is running you have a virtual page address, which you either might not be able to touch without kernel privileges after program completion. Or, there is another reason.
回答8:
It certainly doesn't survive beyond program termination. The idea is to free memory when not needed anymore so that your program doesn't waste memory (it doesn't consume more then it really needs) or, even worse, doesn't run out of memory (depending on your allocation pattern)
回答9:
You have to worry about it because imagine that you were allocating a lot of memory in many many places and NOT freeing it. Once memory is allocated it occupies a portion of memory that cannot be allocated anymore. This will result in the amount of available memory getting smaller and smaller each time because you are failing to free it. At some point the memory will be exhausted. Even if the memory is freed at program termination, imagine that your program runs for several weeks at a time, constantly allocating memory but never freeing it. Memory is a finite resource and you need to be responsible when using dynamic allocation.
来源:https://stackoverflow.com/questions/11383401/can-a-memory-block-allocated-by-using-operator-new-malloc-persist-beyond-end-of