Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* is free() lazy ?
@ 1997-09-17  4:00 Pierre Monestie
  1997-10-01  4:00 ` Al Simons
  0 siblings, 1 reply; 2+ messages in thread
From: Pierre Monestie @ 1997-09-17  4:00 UTC (permalink / raw)



Hi,
If in a c program I allocate a certain amount of memory 
and then I fred it, when I do a ps on my program I realize 
that the prog is taking (RSS value) the amount of memory 
I previously allocated. I know this memory is free but the
 system don't get it back. Is there a way (A kernel config?) to really
"free" this memory.
Nb : if i run my program on a linux pc, the memory is effectively freed.

I'm running DU40B.

void main(){
  int i;
  char *c;
  int sz=1000000;
/*Allocation*/
  c=(char*)malloc(sizeof(char)*sz);
/*Init*/
  for (i=0;i<sz;i++)
    c[i]='f';

  printf("Allocation done, enter a number\n");
  scanf("%d",&i);
  free(c);
  printf("free executed, enter a number\n");
  scanf("%d",&i);

}
thanks.
Pierre




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: is free() lazy ?
  1997-09-17  4:00 is free() lazy ? Pierre Monestie
@ 1997-10-01  4:00 ` Al Simons
  0 siblings, 0 replies; 2+ messages in thread
From: Al Simons @ 1997-10-01  4:00 UTC (permalink / raw)



Pierre Monestie wrote:
> 
> Hi,
> If in a c program I allocate a certain amount of memory
> and then I fred it, when I do a ps on my program I realize
> that the prog is taking (RSS value) the amount of memory
> I previously allocated. I know this memory is free but the
>  system don't get it back. Is there a way (A kernel config?) to really
> "free" this memory.
> Nb : if i run my program on a linux pc, the memory is effectively freed.

There are two items getting in your way.

The first is that free() is a little bit lazy.  It delays freeing a
chunk of memory until the next call to free.  This is to accommodate a
large number of buggy applications which call realloc after calling free
on the chunk of memory.

But that is not the primary cause of the problem you are seeing. 
DIGITAL UNIX's kernel has the default behavior that memory allocated by
sbrk() is not actually removed from the process's virtual address space
with a call to sbrk() with a negative value.  This is a performance
driven decision.  Since it is not removed from the VAS, your RSS and
swapfile requirements track the peak usage of your application.

There is a way to override this: use dbx -k to change the value of
kernel variable old_obreak to zero.  This will cause the kernel to
respect sbrk() calls with a negative value.

Note that once the kernel variable is set to zero, the conditions under
which memory is returned to the system (removed from the process address
space) are controlled by three malloc tuning parameters: __noshrink,
__minshrink and __minshrinkfactor.  See the malloc man page for more
information on these parameters and their use.

Al Simons
DIGITAL UNIX Development Environment Group
simons@zk3.dec.com



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-10-01  4:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-09-17  4:00 is free() lazy ? Pierre Monestie
1997-10-01  4:00 ` Al Simons

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox