3

What happens if I allocate some memory during execution but never call delete and the program terminates? Will the OS free all memory I allocated and no memory will be 'wasted'? Or will I lose a portion of memory until the computer is restarted?

Obviously I know good coding practice is to make sure you delete what you don't need, so I'm not just asking "what is the point of delete"; I'm simply interested to know what happens inside my RAM.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • To free your memory so the operating system can use it again. When your program terminates, all that memory will be automatically freed, however, it is worth free'ing so the memory usage over the life of your process to keep it having a low(er) memory footprint. – RageD Feb 20 '13 at 18:32
  • Thanks that's exactly what I thought happened. – FreelanceConsultant Feb 20 '13 at 18:32
  • 2
    one of questions, that you should... FORGET IT. Just... new and delete. always. always! ALWAYS! – gaussblurinc Feb 20 '13 at 18:35
  • Yeah please read the question again. I already said I know. – FreelanceConsultant Feb 20 '13 at 18:36
  • I just put it here: [what if...](http://what-if.xkcd.com/) – gaussblurinc Feb 20 '13 at 18:37
  • No I dont think that is a good idea – FreelanceConsultant Feb 20 '13 at 18:38
  • But you are brave enough to ask this kind of questions here :) – gaussblurinc Feb 20 '13 at 18:39
  • Don't rely on the OS cleaning up memory for you. While the bulk of OSes will do that, those without virtual memory managers - such as many real-time OSes - will not do that. Furthermore even if the OS cleans up memory for you, you could potentially run into multiple memory leaks during run-time if the same memory allocation is hit multiple times. – Void - Othman Feb 20 '13 at 18:43
  • @gaussblurinc please, really, this kind of question *should* be asked in SO. Just new and delete, not even knowing what happens if we don't do that, is not a good way to learn programming... Yes, you might be a good, experienced programmer, but newbies should know *'why'*, not only the best practice itself :) – starriet Aug 02 '22 at 02:43
  • @starriet I think by his comment he meant not that "you should not ask such a question" but that you are "brave to ask such a question considering the typical response that one would expect from most of the people using this website"... Which would be expected to be generally hostile without any real reason for being so. Users become angry when a question is too simple. They become angry when a question is too complicated. They become angry when they do not understand the question being asked. etc etc... – FreelanceConsultant Aug 02 '22 at 10:31

3 Answers3

10

Conceptually, you leak memory.

In practice, though, if you have a sane OS, it will most likely clean up after your process. But don't rely on that, really.

8

It all depends on what your program does, and how much memory it uses - at any given time and overall throughout its runtime.

Say for example you write a mail server, that allocates memory to hold each email it receives. But it doesn't actually need to store every email that goes through it. So after a few days of receiving and forwarding emails, the mail server can't allocate any more memory, it has consumed every available byte of memory - but it's not actually doing anything useful with that memory, since the emails are no longer in use - they have been dealt with.

On the other hand, if we write our mail server program that allocates, say, 1 MB of buffer to read in an email, process it, and when it's done with, the memory is reused for another email, then there's little point in freeing that memory, ever.

And if we write a program that just reads a file, loading the entire file into memory, and when it has counted all the letters for statistical purposes, prints the stats and exits, whether that allocates a lot of memory or not doesn't really matter.

Of course, all this assumes that the call to new is SIMPLY to allocate memory. If the constructor of the object does something more complex, like open a file, acquire a lock or something else, then all sorts of bad things could happen pretty soon if the destructor isn't called.

The OS will (for the commonly used OS's such as Linux, macOS, iOS, other Unix-systems, Windows, DOS, OS/2, etc) free the memory used by your application.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

The operating systems frees all memory allocated to a process when the process terminates. So the memory allocated by your program will be freed and reclaimed by the operating system once the program terminates.

  • 4
    That's only the case for OSes with virtual memory managers - the bulk of OSes in use today - but it doesn't hold for some real-time OSes, for example. – Void - Othman Feb 20 '13 at 18:37