Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 7 cleans up C++ memory leaks?

Just for fun I created a project that created about 5 GB of memory and did not delete it. As long as the application is running that "memory leak" is there. The second I close my application the memory within 2 seconds is back down to normal as if my program never ran. So the questions have to be asked.

Does Windows 7 clean up memory leaks from bad programs when they are done?

Do all Windows versions do this?

Will Linux and Mac OS X environments do this?

like image 491
Landin Martens Avatar asked Apr 03 '12 04:04

Landin Martens


3 Answers

When the program terminates, the operating system reclaims all the memory that had previously been allocated to it. Cleaning up memory leaks may be a perceived by-product of this, but the OS does not actually see it that way. It does not know that the program had been leaking memory, just that it had allocated it.

like image 139
Philip Sheard Avatar answered Oct 09 '22 11:10

Philip Sheard


Once a process in which your application runs exits, the OS reclaims all the memory allocated to the process.

This is typically true for all operating systems not just Windows 7 or Windows for that matter.

Note that, you may observe different behavior for other leaking resources like file handles etc, usually OS'es dont reclaim those. So, it is usually(Yes,there are exceptions) a good practice to make your own application clear the mess(deallocate the allocated resource) that it made instead of delegating it to the OS.

like image 38
Alok Save Avatar answered Oct 09 '22 11:10

Alok Save


Not only does the program manages memory but does the OS too. And it reclaims all the memory allocated to a program after it exist. It doesn't interfere in between the execution of the program (Other than for paging and swapping). This control over memory of the OS helps the OS from crashing from memory leaks to a point.

Memory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed. This is critical to the computer system.

BSD Unix normally starts reclaiming memory when the percentage of free memory drops below 5% and continues reclaiming until the free memory percentage reaches 7%

like image 41
Rohit Vipin Mathews Avatar answered Oct 09 '22 13:10

Rohit Vipin Mathews