115

Let's say I have the following C code:

int main () {
  int *p = malloc(10 * sizeof *p);
  *p = 42;
  return 0;  //Exiting without freeing the allocated memory
}

When I compile and execute that C program, ie after allocating some space in memory, will that memory I allocated be still allocated (ie basically taking up space) after I exit the application and the process terminates?

3
  • 11
    it's "good style" to clean up your memory, not because you might run on an OS that doesn't have protected memory (which is the main suggestion below), but because it increases the likelihood you'll find memory leaks, and keeps your code lean and correct... Feb 6, 2010 at 16:42
  • I know it doesn't really matter since it's just an example, but why allocate memory for 10 ints if you're only storing one?
    – Sparkette
    Jan 1, 2022 at 18:19
  • Does this answer your question? What REALLY happens when you don't free after malloc before program termination?
    – bkausbk
    May 10, 2023 at 13:33

9 Answers 9

145

It depends on the operating system. The majority of modern (and all major) operating systems will free memory not freed by the program when it ends.

Relying on this is bad practice and it is better to free it explicitly. The issue isn't just that your code looks bad. You may decide you want to integrate your small program into a larger, long running one. Then a while later you have to spend hours tracking down memory leaks.
Relying on a feature of an operating system also makes the code less portable.

19
  • 21
    I once encountered win98 on an embedded platform, and based off of that experience, I can say that it does NOT free memory when programs close. Feb 6, 2010 at 15:44
  • 11
    @Ken It was an example. Also, there is a line between YAGNI and sloppy coding. Not freeing resources crosses it. The YAGNI principle was also meant to be applied to features, not code that makes the program work correctly. (And not freeing memory is a bug).
    – Yacoby
    Feb 6, 2010 at 15:53
  • 5
    +1: the most important thing to consider is that memory management is as Yacoby quite correctly states: "a feature of the operating system". Unless I am mistaken, the programming language does not define what happens before or after program execution.
    – D.Shawley
    Feb 6, 2010 at 15:57
  • 10
    Freeing memory manually takes more time, takes more code, and introduces the possibility of bugs (tell me you've never seen a bug in deallocation code!). It's not "sloppy" to intentionally omit something which is worse in every way for your particular use case. Unless or until you mean to run it on some ancient/tiny system which can't free pages after process termination, or integrate it into a larger program (YAGNI), it looks like a net loss to me. I know it hurts a programmer's ego to think of not cleaning it up yourself, but in what practical way is it actually better?
    – Ken
    Feb 6, 2010 at 16:56
  • 6
    Anyone proposing memory leaks on SO should be stripped of all reputation and badges
    – Ulterior
    May 17, 2012 at 21:38
60

In general, modern general-purpose operating systems do clean up after terminated processes. This is necessary because the alternative is for the system to lose resources over time and require rebooting due to programs which are poorly written or simply have rarely-occurring bugs that leak resources.

Having your program explicitly free its resources anyway can be good practice for various reasons, such as:

  • If you have additional resources that are not cleaned up by the OS on exit, such as temporary files or any kind of change to the state of an external resource, then you will need code to deal with all of those things on exit, and this is often elegantly combined with freeing memory.
  • If your program starts having a longer lifetime, then you will not want the only way to free memory to be to exit. For example, you might want to convert your program into a server (daemon) which keeps running while handling many requests for individual units of work, or your program might become a small part of a larger program.

However, here is a reason to skip freeing memory: efficient shutdown. For example, suppose your application contains a large cache in memory. If when it exits it goes through the entire cache structure and frees it one piece at a time, that serves no useful purpose and wastes resources. Especially, consider the case where the memory pages containing your cache have been swapped to disk by the operating system; by walking the structure and freeing it you're bringing all of those pages back into memory all at once, wasting significant time and energy for no actual benefit, and possibly even causing other programs on the system to get swapped out!

As a related example, there are high-performance servers that work by creating a process for each request, then having it exit when done; by this means they don't even have to track memory allocation, and never do any freeing or garbage collection at all, since everything just vanishes back into the operating system's free memory at the end of the process. (The same kind of thing can be done within a process using a custom memory allocator, but requires very careful programming; essentially making one's own notion of “lightweight processes” within the OS process.)

15

My apologies for posting so long after the last post to this thread.

One additional point. Not all programs make it to graceful exits. Crashes and ctrl-C's, etc. will cause a program to exit in uncontrolled ways. If your OS did not free your heap, clean up your stack, delete static variables, etc, you would eventually crash your system from memory leaks or worse.

Interesting aside to this, crashes/breaks in Ubuntu, and I suspect all other modern OSes, do have problems with "handled' resources. Sockets, files, devices, etc. can remain "open" when a program ends/crashes. It is also good practice to close anything with a "handle" or "descriptor" as part of your clean up prior to graceful exit.

I am currently developing a program that uses sockets heavily. When I get stuck in a hang I have to ctrl-c out of it, thus, stranding my sockets. I added a std::vector to collect a list of all opened sockets and a sigaction handler that catches sigint and sigterm. The handler walks the list and closes the sockets. I plan on making a similar cleanup routine for use before throw's that will lead to premature termination.

Anyone care to comment on this design?

2
  • 2
    I'm glad you said this, because I had a program that left socket resources around, and our Ubuntu system needed rebooting every two weeks or memory started running out, and there is plenty of memory. I'm not sure system resources are torn down if you forget to clean them up. Jul 8, 2012 at 21:29
  • 12
    Stackoverflow isn't a forum; there is nothing wrong with answering an old question. meta.stackexchange.com/questions/20524/reviving-old-questions
    – mk12
    Aug 16, 2012 at 1:45
9

What's happening here (in a modern OS), is that your program runs inside its own "process." This is an operating system entity that is endowed with its own address space, file descriptors, etc. Your malloc calls are allocating memory from the "heap", or unallocated memory pages that are assigned to your process.

When your program ends, as in this example, all of the resources assigned to your process are simply recycled/torn down by the operating system. In the case of memory, all of the memory pages that are assigned to you are simply marked as "free" and recycled for the use of other processes. Pages are a lower-level concept than what malloc handles-- as a result, the specifics of malloc/free are all simply washed away as the whole thing gets cleaned up.

It's the moral equivalent of, when you're done using your laptop and want to give it to a friend, you don't bother to individually delete each file. You just format the hard drive.

All this said, as all other answerers are noting, relying on this is not good practice:

  1. You should always be programming to take care of resources, and in C that means memory as well. You might end up embedding your code in a library, or it might end up running much longer than you expect.
  2. Some OSs (older ones and maybe some modern embedded ones) may not maintain such hard process boundaries, and your allocations might affect others' address spaces.
4

Yes. The OS cleans up resources. Well ... old versions of NetWare didn't.

Edit: As San Jacinto pointed out, there are certainly systems (aside from NetWare) that do not do that. Even in throw-away programs, I try to make a habit of freeing all resources just to keep up the habit.

4
  • 3
    I'm not downvoting, but this is a pretty dangers post for posterity. DOS is still used on many embedded platforms, and I SERIOUSLY doubt that it does the memory cleanup for you. The sweeping generalization is wrong. Feb 6, 2010 at 15:54
  • @San Jacinto: That is a good point. That is why I did make the NetWare reference, but it probably could use clarification. I'll edit it a bit. Feb 6, 2010 at 16:15
  • 4
    @San DOS is not a multi-tasking OS - when a DOS program (excluding TSRs) ends, all the memory is available for the next program to be loaded.
    – anon
    Feb 6, 2010 at 16:22
  • @Neil thanks for the reminder, but I was referring to a TSR-like program that would launch when an event occurs, as is a common use for embedded systems. Nonetheless, thank you for your expertise and clarification where I failed :) Feb 6, 2010 at 16:32
2

Yes, the operating system releases all memory when the process ends.

2
  • I don't see why this was downvoted. malloc'ed memory will be released when the process dies (the wikipedia definiton of malloc says so)
    – Arve
    Feb 6, 2010 at 16:13
  • 7
    Wikipedia is not the manual for every OS in existence. Most modern OSes will reclaim the memory, but not all (and particularly not all old ones) do. Add to that, malloc can only promise what C will do with the memory; by design, C doesn't guarantee much of anything regarding behavior outside of C itself. If the app dies unexpectedly, any promises the runtime library made are null and void, since it is no longer alive to live up to them.
    – cHao
    Jul 23, 2012 at 1:20
2

It depends, operating systems will usually clean it up for you, but if you're working on for instance embedded software then it might not be released.

Just make sure you free it, it can save you a lot of time later when you might want to integrate it in to a large project.

0

That really depends on the operating system, but for all operating systems you'll ever encounter, the memory allocation will disappear when the process exits.

0

I think direct freeing is best. Undefined behaviour is the worst thing, so if you have access while it's still defined in your process, do it, there are lots of good reasons people have given for it.

As to where, or whether, I found that in W98, the real question was 'when' (I didn't see a post emphasising this). A small template program (for MIDI SysEx input, using various malloc'd spaces) would free memory in the WM_DESTROY bit of the WndProc, but when I transplanted this to a larger program it crashed on exit. I assumed this meant I was trying to free what the OS had already freed during a larger cleanup. If I did it on WM_CLOSE, then called DestroyWindow(), it all worked fine, instant clean exit.

While this isn't exactly the same as MIDI buffers, there is similarity in that it is best to keep the process intact, clean up fully, then exit. With modest memory chunks this is very fast. I found that many small buffers worked faster in operation and cleanup than fewer large ones.

Exceptions may exist, as someone said when avoiding hauling large memory chunks back out of a swap file on disk, but even that may be minimised by keeping more, and smaller, allocated spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.