Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does windows automatically free up memory when program closes (not returning from main)? [duplicate]

Tags:

c++

memory

Does the new memory allocated always get freed up when program closes? (even if closes unexpectedly from a bug/error etc, or custom close functions)?

Or does it only free the memory if it returns from main?

like image 869
Tez Avatar asked Apr 14 '13 10:04

Tez


People also ask

Is memory automatically freed when a program exits?

Most memory on most OSes however will be automatically reclaimed when the process exits.

Does heap memory get deallocated after program execution?

Everything gets deallocated when the program exits, by the operating system, not the compiler.

What happens if memory is not freed correctly?

In these cases, even small chunks of storage add up and create a problem. Thus our usable space decreases. This is also called “memory leak”. It may also happen that our system goes out of memory if the de-allocation of memory does not take place at the right time.

Why do you have to free memory in C?

When your program ends all of the memory will be freed by the operating system. The reason you should free it yourself is that memory is a finite resource within your running program. Sure in very short running simple programs, failing to free memory won't have a noticable effect.

Does the system free up memory when a program exits?

Are you running on a desktop OS (Windows, Linux etc.)? If so, yes, in general the system will free any memory associated with the program when the program exits.

Does Microsoft Auto close apps that use over 50% Ram?

In both 2 cases, i think microsoft auto set when use 50% of ram, system will think low memory and auto close app when use over 60% ram. I said i saw ram free in task manager, not use bios ram.

What happens to the memory when the program is terminated?

However it is important to note that in specialized environments such as various Real-Time Operating Systems the memory may not be freed when the program is terminated. Show activity on this post.

How do I restore free memory in Windows 10?

Click the Performance tab and the Resource Monitor button. Click the Memory tab and check whether Free is near to Zero or is Zero. If it is, open RAMMap, click Empty on the Menu bar and Empty Standby list. This action instantly restores Free memory.


1 Answers

Yes, operating systems usually keep track of the memory allocated by each process and release it when those processes terminate - no matter how.

This, however, is not a valid reason to have memory leaks in your program in general: a program should always actively release the resources (including memory) it acquires, unless there is a really good - and documented - reason for not doing so.

Good reasons could be the dependency of a program's correctness on the order of destruction of global/singleton objects, or the expensiveness of actively freeing allocated memory before termination.

However, while admitting that there could be reasons why a programmer would intentionally avoid releasing memory, please be careful not to develop a too shallow mindset as to what counts as a "good reason" for not cleaning after yourself.

I would encourage you to get used to write code that does release the memory it acquires, and document explicitly in a very clear form every situation where you are not going to follow this practice. Again, while there might be corner-cases that require this, releasing or not releasing acquired memory always has to be an active, intentional decision of the programmer.


NOTE: Quoting Steve Jessop from the comments, another good reason why you would not want to actively release memory is when your program needs to be terminated because it somehow reached an unexpected state - perhaps one that violates an invariant, or a pre-condition of a certain function. Usually, violating a precondition means Undefined Behavior.

Since - by definition - there is no sane way to recover from UB, you may want to immediately terminate your program rather than performing further actions that could have any outcome - including highly undesirable ones.

like image 129
Andy Prowl Avatar answered Oct 15 '22 07:10

Andy Prowl