1

I read malloc function allocates memory on the heap, where heap resides in virtual memory in OS(Linux). So I have few doubts:

If process who is using memory allocation by malloc is terminated by either kill or exit itself without deallocating memory. Will that memory be deallocated by OS after termination as it is in virtual memory?

  • How can I know heap size in Linux OS?
  • How can I change heap size in Linux OS?
Maroun
  • 94,125
  • 30
  • 188
  • 241
Embedded Programmer
  • 519
  • 4
  • 8
  • 22
  • 1
    "If process who is using memory allocation by malloc is terminated by either kill or exit itself without deallocating memory. Will that memory be deallocated by OS after termination as it is in virtual memory?" YES, all OS's do this (except some for embedded devices etc.). – Marius Jun 27 '13 at 07:22
  • do you mean memory allocated from heap in embedded device(in which there is no virtual memory) get killed or exit without deallocating create memory leak problem? but one who has virtual memory like ubuntu PC, will be deallocated by OS, but does not depends on process? – Embedded Programmer Jun 27 '13 at 07:35
  • Every OS will deallocate ANY ressources an application has requested if the application is ended (or force-closed). If you work with an OS that does not, you'd know that, trust me. – Marius Jun 27 '13 at 07:47
  • Is it means, there will not be any memory leakage problem is Linux OS? – Embedded Programmer Jun 27 '13 at 10:38
  • If you produce memory-leaks inside your application, then your application simply has lost track of these blocks of memory. However, Linux still knows the block is assigned to the process running your program, so it takes care of it when the process is ended. Ressource management is an essential part of any OS. – Marius Jun 27 '13 at 12:23
  • Whatever I understand,memory leakage is not problem after task termination as OS will clear it. But application who is using dynamic memory allocation will be problematic as it can't access to leaked memory. Am I right – Embedded Programmer Jun 27 '13 at 12:30
  • Maybe you should do some research on your own? http://en.wikipedia.org/wiki/Memory_leak – Marius Jun 27 '13 at 12:42

2 Answers2

1

Will that memory be deallocated by the OS after the termination?
Yes it will, but I won't really call that deallocation(as in, no one will be calling free() after all your allocations); what happens is that the virtual address space assigned to your process (including the stack, the heap, the code, .bss, and any other segement) simply gets removed from the OS so any physical memory areas that were mapped to your process virtual memory will be usable by anyone else (without the need to swap in/out). For more information about that, read this excellent article.


How can I know heap size in Linux OS? ulimit -m
How can I change the heap size? ulimit -S -m X (where X is the heap limit in kilo bytes)
For a more thorough explanation, visit this SO question.

Community
  • 1
  • 1
Fingolfin
  • 5,363
  • 6
  • 45
  • 66
0

The memory allocated to a process is freed when it gracefully or otherwise terminates. To set/check the heap size use ulimit:

ulimit -m              # shows heap per process
ulimit -S -m 1000      # set heap size to 1000 * 1024 bytes
ulimit -S -m unlimited # unlimited heap size
perreal
  • 94,503
  • 21
  • 155
  • 181