25

If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS?

I run in Linux environment.

Niall C.
  • 10,878
  • 7
  • 69
  • 61
SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • 2
    I asked it out of pure curiosity and to understand the working of memory allocation and OP better, I promise to all the concerned answerers (those who answered) that I'll always free my allocated memory. – SIMEL Apr 10 '11 at 13:59
  • possible duplicate of [What REALLY happens when you don't free after malloc?](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – mmmmmm May 12 '13 at 13:24

8 Answers8

22

Any modern operating system will clean up everything after a process terminates, but it's generally not a good practice to rely on this.

It depends on the program you are writing. If it's just a command line tool that runs and terminates quickly, you may not bother cleaning up. But be aware that it is this mindset that causes memory leaks in daemons and long-running programs.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 3
    I totally get your point, but I disagree that the mindset of "not cleaning up memory when my process exits" will cause leaks in long-running programs. It doesn't matter how long it runs, the OS will clean it up on exit just the same. "Being lazy about cleanup" will certainly cause memory leaks, but that's not the question. The decision not to free memory should be conscious and deliberate. – Dan Bechard Jul 14 '17 at 15:58
13

It can be a good design and very efficient to simply exit and allow the operating system to clean everything up. Apple OS X now does this by default: applications are killed without notice unless the application sets a "don't kill me" flag.

Often, freeing every memory allocation takes significant time. Some memory pages may have been swapped out and must be read back in so they can be marked as free. The memory allocator has to do a lot of work updating free memory tracking data. All of this effort is a waste because the program is exiting.

But this must be done by design and not because the programmer has lost track of allocated memory!

Community
  • 1
  • 1
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
3

In any case it will be freed by the operating system upon process termination. So you don't need it, but since it is a good practice, why don't you do it anyway? :)

Actually with complex code I wouldn't risk to don't release something which I'm not sure at 100% that will be useless because program exits afterwards. So for any minimal doubt just free it.

Jack
  • 131,802
  • 30
  • 241
  • 343
2

The operating system will reclaim the memory so you don't need to free it.

Most programs do free memory though because if you don't free any memory then you are liable to have problems caused by these intentional leaks.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • the downvote was from me. But I could swear there was only the first sentence when I did. I'd take it back now with the added sentence, but it's somehow locked unless it gets edited again or whatever that means. Any advice appreciated how to remove the downvote now after your change. Downvoted a second (now deleted) answer in this topic for the same reason, btw. – 0xC0000022L Apr 10 '11 at 14:17
  • @status you can change your vote now if you want. What's wrong with the first sentence? It's true don't you know?! – David Heffernan Apr 10 '11 at 14:24
  • thanks. Done. It's true for most operating systems, yes. But it does not consider that code written can be reused in other places (think libraries) where the freeing *is* vital. Which is *why* it's good practice to free anything even if the OS collects the memory by default when the process terminates. – 0xC0000022L Apr 10 '11 at 14:27
  • @status question was about processes terminating on linux. For what it's worth I always free my memory but I know that my app would shut down quicker if I did not! – David Heffernan Apr 10 '11 at 14:30
  • Can you elaborate on "problems caused by these intentional leaks"? What problems might this cause, assuming the leaks *only* occur at exit time? – Dan Bechard Jul 14 '17 at 16:01
  • Running out of memory or worse, address space – David Heffernan Jul 14 '17 at 16:07
2

Yes you can assume that.

Although it is a good practice to deallocate the memory immediately after it is not needed, even for software that runs for a short time only.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

Linux will free the allocated memory and close the file descriptors on process termination.

Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
0

The OS will reclaim the memory, however it's good practice to free things if you expect they'll run out of scope before you malloc something else. However, you can more or less rely upon the termination of the program to deal with memory management for you.

Ben Stott
  • 2,218
  • 17
  • 23
0

Always free your allocated memory since that the operating system will hold less memory for no reason. It is very noticed in small operating systems that holds small memory size.

Batman
  • 1,244
  • 2
  • 14
  • 26
  • This is a poor answer that doesn't address the actual question, which is about whether memory is freed upon exiting from a Linux process. It is freed by the OS, and explicitly freeing all memory before exiting pointlessly slows and complicates programs. – Jim Balter Apr 12 '11 at 08:57