11

I have written a C program containing code allocating memory using the malloc() function. If I don't free up the memory using the free() function, will Ubuntu release it automatically?

  • It has nothing to do with Ubuntu, its kernel's responsibility. – user May 19 '15 at 0:49
  • @user: A particular version of Ubuntu must run on top of one of the versions of Linux kernel. So it has something to do with Ubuntu so as to know whether the kernel it is running on releases the memory automatically or not. – ashubuntu May 19 '15 at 13:19
15

Yes.

The kernel will release all the resources allocated by a program upon the program's termination.

This is done after the do_exit() function defined in kernel/exit.c has terminated its execution; the do_exit() function execution itself might be triggered by a number of events, most commonly by an explicit exit() syscall called by the program during its regular computation or by an implicit exit() syscall called by the program upon its termination (as the C compiler places an exit() syscall after main()'s return). Other reasons include the reception of an unhandable / not ignorable signal or exception.

The do_exit() function itself performs a number of tasks. After it has terminated its execution, the program's allocated resources are released by the kernel and returned to the system for further uses.

Source

  • 7
    But that is of course no excuse for "forgetting" the free() ... – Hagen von Eitzen May 14 '15 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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