DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Question about free()

  1. #1
    WL Guest

    Question about free()


    Hi all,
    Sorry I know this is a Windows forum while I am using Solaris 8 and GCC to
    do my project. One question I would like to ask is that after I call free(ptr);
    the OS doesn't seem regaining the memory, the total memory available remains
    the same. According to my colleague, he said that that block of memory only
    return to the heap but not to the OS until the program finishes it execution.
    Is it correct, if so how could I force the program to return the freed memory
    back to the OS? In Windows, I found that there is an API called _heapmin()
    that does this job, but I couldn't find one in Unix.
    Any suggestions are welcomed.

    ~~~~~~~~
    char *ptr;
    ptr = malloc (1024);
    free(ptr);
    ~~~~~~~~

    Thanks in advance.

  2. #2
    Danny Kalev Guest

    Re: Question about free()

    First of all this *isn't* a Windows forum but a general C/C++ forum so
    your question is in place.
    Solaris uses a special heap manager that merely marks the freed memory
    as freed but doesn't return it to the free memory pool; this way, the
    system avoids heap fragmentation. If your program allocates another 1024
    byte chunk of memory after calling free(), you will see that the newly
    allocated block has the same address as the previous block. I believe
    there is a way to force the system to reclaim fred memory but you have
    to use Solaris proprietary APIs; not malloc and free.

    Danny



    WL wrote:
    >
    > Hi all,
    > Sorry I know this is a Windows forum while I am using Solaris 8 and GCC to
    > do my project. One question I would like to ask is that after I call free(ptr);
    > the OS doesn't seem regaining the memory, the total memory available remains
    > the same. According to my colleague, he said that that block of memory only
    > return to the heap but not to the OS until the program finishes it execution.
    > Is it correct, if so how could I force the program to return the freed memory
    > back to the OS? In Windows, I found that there is an API called _heapmin()
    > that does this job, but I couldn't find one in Unix.
    > Any suggestions are welcomed.
    >
    > ~~~~~~~~
    > char *ptr;
    > ptr = malloc (1024);
    > free(ptr);
    > ~~~~~~~~
    >
    > Thanks in advance.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center