OS out of memory if malloc without free?

This is a discussion on OS out of memory if malloc without free? within the C Programming forums, part of the General Programming Boards category; Dear all, If a C program malloc'ed some memory, but somehow exited without calling free(), will the malloc'ed memory be ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Question OS out of memory if malloc without free?

    Dear all,

    If a C program malloc'ed some memory, but somehow exited without calling free(), will the malloc'ed memory be freed by the OS automatically?

    If the answer is no, then does it means after running that program for a number of times, the OS will run out of memory?

    TIA,
    Michael

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    On exit OS cleansup everything.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    > On exit OS cleansup everything.
    But I think he means, running the program without exiting the other instance...

    Actually in a modenr OS (like Linux), you can limit the amount of memory each user can use. So, you can prevent your system runnig out of memory by setting such a limit.

    On the other hand, if you don't have a limit, yes it is possible to get out of memory because when the process requests some dynamic memory from the OS, OS allocates memory from the process' address space. If the process' address space cannot handle more memory allocation request, memory manager request some more memory from the OS kernel. And if the system has enough memory, it is allocated for the process (the process address space is extended).

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Thanks for the quick responses! I am talking about the program exited, no other instance running.

    I asked this question as I am new to C, and I cannot find document/guides that tell me on exit the OS will free all alloc'ed memory.

  5. #5
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    @fnoyan : Yes ur right. But at any case an instance or a process exits OS cleansup it process space.
    So the answer to question
    If a C program malloc'ed some memory, but somehow exited without calling free(), will the malloc'ed memory be freed by the OS automatically?
    answer should be yes.
    what say u?

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Yes, for a modern OS. Since there is no more any process descriptor for the process, no resources can be hold by the OS on behalf of the process.

    But you may want to read this thread

    General Malloc question

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Thank God (of the C) and everybody! Now I know what to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. When to free up memory?
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 09-27-2005, 03:22 PM
  4. How to free memory in this program
    By Coconut in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21