memeory freed by free() not reflected in 'ps'

memeory freed by free() not reflected in 'ps'

Post by Ze Ya » Mon, 10 Nov 1997 04:00:00



If I allocate a linked list, then free the whole list nodes one by one,
it seems that under Linux, only after the whole list is freed, the 'SZ'
field of 'ps' command is resumed to the level before the list is created.
Even if only one node is left, the 'SZ' field is not any different than
the whole list is there. Under Solaris, even if the whole list is freed,
the 'SZ' field does not resume. Follwoing is my test program:

Anyone knows why?

-------------------------------> test.c <---------------------
#include <stdio.h>
#define SIZE 9000

typedef struct node_cell *pNode;

typedef struct node_cell {
        int     dumy;
        int     dumy2;
        pNode   next;

Quote:} Node;

main()
{
 int i;
 Node *head, *test, *new;

 system("ps u | grep a.out");

 test = (Node *) malloc(sizeof(Node));
 head = test;

 for(i=0; i<SIZE; i++)
 {
   new = (Node *) malloc(sizeof(Node));    

   test->next = new;
   test = new;
 }
 new->next = 0;

 system("ps u | grep a.out");

 i=0;
 while(head)
 {
   i++;
   new = head;
   head = head->next;
   free(new);
   if(i== SIZE)
   system("ps u | grep a.out");
 }

   system("ps u | grep a.out");

Quote:}

 
 
 

memeory freed by free() not reflected in 'ps'

Post by Marc Slemk » Tue, 11 Nov 1997 04:00:00



Quote:>If I allocate a linked list, then free the whole list nodes one by one,
>it seems that under Linux, only after the whole list is freed, the 'SZ'
>field of 'ps' command is resumed to the level before the list is created.
>Even if only one node is left, the 'SZ' field is not any different than
>the whole list is there. Under Solaris, even if the whole list is freed,
>the 'SZ' field does not resume. Follwoing is my test program:
>Anyone knows why?

Not returing memory to the system after it is free()ed is normal
behavior on most operating systems.  Some systems, such as
Linux and FreeBSD (among others) sometimes return it to the system,
although they can't always do so.

Most systems don't, but keep it for reuse.

 
 
 

memeory freed by free() not reflected in 'ps'

Post by Vlad 'Borg' Peterse » Tue, 11 Nov 1997 04:00:00


........

Quote:> If you are concerned about leaking memory, you could try using
> a tool like purify on solaris.   If that's out of your price
> range, I do remember seeing an article in Dr. Dobb's Journal
> some years ago which showed how you could build a shell around
> the malloc/free calls to track memory usage.   Failing that,
> I can offer only the old adage:  "read your code carefully."

Electric Fence library for malloc()/free() debugging
by Bruce Perence is great for that kind of thing. It's
under GPL.

--
My real email address is:      <vladimip at uniserve dot com>
 #include <disclaimer.h> |   *Good pings come in small packets*
     Vancouver, BC       |    Windows: for IQs smaller than 95
      SIGSIG -- signature too long (core dumped)

 
 
 

memeory freed by free() not reflected in 'ps'

Post by Gary Luc » Tue, 11 Nov 1997 04:00:00



> If I allocate a linked list, then free the whole list nodes one by one,
> it seems that under Linux, only after the whole list is freed, the 'SZ'
> field of 'ps' command is resumed to the level before the list is created.
> Even if only one node is left, the 'SZ' field is not any different than
> the whole list is there. Under Solaris, even if the whole list is freed,
> the 'SZ' field does not resume. Follwoing is my test program:

Under solaris, the process size never shrinks.  Any memory you free is
simply put on an internal free list and kept ready for future
use by the process.   Memory is not returned to the system until
the process terminates.   It's quite efficient if you're doing a
lot of allocates and frees....

I haven't used Linux and I wouldn't have guessed that it COULD
return memory to the system.   I imagine that what's happening
is that a process can only return "high memory" (memory
with high-valued addresses).   As long as you have something
residing in high-memory, none of the free space beneath it can
be returned.   So it's not until you free your last piece of the
linked list that the memory actually becomes eligable for return
to the system.  

If you are concerned about leaking memory, you could try using
a tool like purify on solaris.   If that's out of your price
range, I do remember seeing an article in Dr. Dobb's Journal
some years ago which showed how you could build a shell around
the malloc/free calls to track memory usage.   Failing that,
I can offer only the old adage:  "read your code carefully."

                                        gwl

--
***********************************************************************
Computer Programming is the Art of the Possible
Gary Lucas                                              Sonalysts, Inc.

Work: (860)442-4355                                 Waterford CT 06385
***********************************************************************