SIMEL
SIMEL

Reputation: 8939

Is freeing allocated memory needed when exiting a program in C

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.

Upvotes: 25

Views: 7688

Answers (8)

Zan Lynx
Zan Lynx

Reputation: 54345

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!

Upvotes: 13

David Heffernan
David Heffernan

Reputation: 613252

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.

Upvotes: 2

Batman
Batman

Reputation: 1324

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.

Upvotes: 0

Rumple Stiltskin
Rumple Stiltskin

Reputation: 10415

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

Upvotes: 1

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43538

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.

Upvotes: 23

Ben Stott
Ben Stott

Reputation: 2218

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.

Upvotes: 0

Šimon Tóth
Šimon Tóth

Reputation: 36441

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.

Upvotes: 2

Jack
Jack

Reputation: 133609

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.

Upvotes: 3

Related Questions