Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to memory that is not freed after end of program? [duplicate]

Duplicate: What REALLY happens when you don’t free after malloc?

Let's say, for example:

int main()
{
  char* test = new char[50000];
  return 0;
}

What happens to the allocated memory after the program had finished? Does it get freed for other applications immediately? Or perhaps after some time? Or maybe it's lost to the system forever? Or does it get swapped to the disk never to return to RAM? Or maybe something completely different?

I would like to know what happens on the major 3 OS's: Windows (XP and up, if there are any differences), Linux, Mac OS X.

like image 701
GhassanPL Avatar asked May 14 '09 15:05

GhassanPL


2 Answers

See: What REALLY happens when you don't free after malloc?

like image 148
TStamper Avatar answered Sep 24 '22 23:09

TStamper


On any O/S with a MMU (which includes Unix, Linux, OSX and the Windows NT family) the process has a data structure that is used to set up page mappings for tbe MMU. When the process is terminated this mapping is released and the pages are added to the Operating System's free pool.

On non protected-memory O/S platforms such as DOS or some realtime operating systems the memory may need to be explicitly freed and the O/S pool could possibly leak memory if it is not tidied up correctly.

like image 42
ConcernedOfTunbridgeWells Avatar answered Sep 24 '22 23:09

ConcernedOfTunbridgeWells