Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* Returning memory to system after a free
@ 2002-08-27 10:37 Jan Sundberg
  2002-08-28  2:26 ` Ian Cairns
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Sundberg @ 2002-08-27 10:37 UTC (permalink / raw)


Hi.

I'm trying to return memory segments to kernel after a free.
This har been tested and verified by using the malloc tuning values
on both 4.0 and 5.1:

  __delayed_free = 0;
  __madvisor = 1;
  __minshrinkfactor = 0.0;
  __minshrink = 1;

in a test program (mem) using malloc and free and 
checking the process RSS with ps on the side.

Running example:

~> mem 2 10000000

20004864 / -33264

where the figures show the break returned by sbrk(0) 
compared to program start value:

- after a first allocation of (2 blocks of 10000000 bytes), and
- after free of these blocks (i e it shrunk a bit)

The RSS was verified during program run (with inserted sleeps):

utvsrv1 ~> ps -o rss -p 32297
 RSS
152K
utvsrv1 ~> ps -o rss -p 32297
 RSS
 19M
utvsrv1 ~> ps -o rss -p 32297
 RSS
136K

Now, trying different sizes and number of blocks yields:

n=$((16*1024*1024)) # 16 M in different ways
d=1
while [[ n -ge 10 ]]; do 
  echo -n $d $n " : "
  ./mem $d $n
  n=$(($n/2))
  d=$(($d*2))
done

1 16777216  : 16785408 / -33264 // 1 block of 16 M
2 8388608  : 16793600 / -33264  // 2 blocks of 8 M
4 4194304  : 16809984 / -33264   // and so on...
8 2097152  : 16842752 / -25072 
16 1048576  : 16908288 / -25072 
32 524288  : 17039360 / -25072 
64 262144  : 16760832 / -25072 
128 131072  : 16850944 / -25072 
256 65536  : 16809984 / -25072 
512 32768  : 16793600 / -25072
1024 16384  : 16785408 / -25072 
2048 8192  : 21078016 / 21078016 // hmmm...
4096 4096  : 21094400 / 21094400 
8192 2048  : 21061632 / 21061632
...


No memory is returned using smaller blocks than 16k !


Further testing yielded:

utvsrv1 ~> mem 128 10233
1351680 / -25056  // returned !
utvsrv1 ~> mem 128 10232
1351680 / 1351680  // not returned !


How come segments smaller than a certain size (10233) never are
returned to the kernel !?!? This is a major drawback since most
segments are relatively small (e g malloc (sizeof (TYPE))).


Sincerely.

/ Jan Sundberg


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

* Re: Returning memory to system after a free
  2002-08-27 10:37 Returning memory to system after a free Jan Sundberg
@ 2002-08-28  2:26 ` Ian Cairns
  2002-08-28  8:08   ` Emmanuel Choquet
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Cairns @ 2002-08-28  2:26 UTC (permalink / raw)


I've had problems returning large quantities of memory when closing down an
application - on the defaults it took over an hour (for about 1.5 Gbyte in
small chunks) - with pre 5.1 systems.

I'll see if the then suggested solution is still valid (the powers that be
may have changed the code to do the sensible(TM) thing) but I'd suggest a
careful perusal of the malloc() man page might help - I think there's an
indication of how to affect the speed of coalescing returned memory in there
somewhere.

[The solution - as it was then - brought the application close down time
down to under a minute from over an hour.]

        Ian.

"Jan Sundberg" <janne@nevdull.nu> wrote in message
news:e7875f7.0208270237.4e0ba29e@posting.google.com...
> Hi.
>
> I'm trying to return memory segments to kernel after a free.
> This har been tested and verified by using the malloc tuning values
> on both 4.0 and 5.1:
>
>   __delayed_free = 0;
>   __madvisor = 1;
>   __minshrinkfactor = 0.0;
>   __minshrink = 1;
>
> in a test program (mem) using malloc and free and
> checking the process RSS with ps on the side.
>
> Running example:
>
> ~> mem 2 10000000
>
> 20004864 / -33264
>
> where the figures show the break returned by sbrk(0)
> compared to program start value:
>
> - after a first allocation of (2 blocks of 10000000 bytes), and
> - after free of these blocks (i e it shrunk a bit)
>
> The RSS was verified during program run (with inserted sleeps):
>
> utvsrv1 ~> ps -o rss -p 32297
>  RSS
> 152K
> utvsrv1 ~> ps -o rss -p 32297
>  RSS
>  19M
> utvsrv1 ~> ps -o rss -p 32297
>  RSS
> 136K
>
> Now, trying different sizes and number of blocks yields:
>
> n=$((16*1024*1024)) # 16 M in different ways
> d=1
> while [[ n -ge 10 ]]; do
>   echo -n $d $n " : "
>   ./mem $d $n
>   n=$(($n/2))
>   d=$(($d*2))
> done
>
> 1 16777216  : 16785408 / -33264 // 1 block of 16 M
> 2 8388608  : 16793600 / -33264  // 2 blocks of 8 M
> 4 4194304  : 16809984 / -33264   // and so on...
> 8 2097152  : 16842752 / -25072
> 16 1048576  : 16908288 / -25072
> 32 524288  : 17039360 / -25072
> 64 262144  : 16760832 / -25072
> 128 131072  : 16850944 / -25072
> 256 65536  : 16809984 / -25072
> 512 32768  : 16793600 / -25072
> 1024 16384  : 16785408 / -25072
> 2048 8192  : 21078016 / 21078016 // hmmm...
> 4096 4096  : 21094400 / 21094400
> 8192 2048  : 21061632 / 21061632
> ...
>
>
> No memory is returned using smaller blocks than 16k !
>
>
> Further testing yielded:
>
> utvsrv1 ~> mem 128 10233
> 1351680 / -25056  // returned !
> utvsrv1 ~> mem 128 10232
> 1351680 / 1351680  // not returned !
>
>
> How come segments smaller than a certain size (10233) never are
> returned to the kernel !?!? This is a major drawback since most
> segments are relatively small (e g malloc (sizeof (TYPE))).
>
>
> Sincerely.
>
> / Jan Sundberg
>




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

* Re: Returning memory to system after a free
  2002-08-28  2:26 ` Ian Cairns
@ 2002-08-28  8:08   ` Emmanuel Choquet
  0 siblings, 0 replies; 3+ messages in thread
From: Emmanuel Choquet @ 2002-08-28  8:08 UTC (permalink / raw)


" 
> "Jan Sundberg" <janne@nevdull.nu> wrote in message
> news:e7875f7.0208270237.4e0ba29e@posting.google.com...
> > Hi.
> >
> > I'm trying to return memory segments to kernel after a free.
> > This har been tested and verified by using the malloc tuning values
> > on both 4.0 and 5.1:

malloc has actually to do only with virtual memory, IE address space.

roughly, the process is the following, in the VM space of the process
:

if the malloc space asked is new - has never been asked - the Mach
kernel and Unix VM algorithm will map the space in your process -
free() will not unmap the space, Mach and Unix level, VSize of the
process will remain the same.

In a next malloc call, within the size of the first call, Mach kernel
and Unix memory allocation have nothing to do, the new space can be on
overlap with the former freed space, VM and Physical.

Now in the physical memory side, you first have 2 cases :

- 'normal' process, address space is created at malloc call, and
Zero-filled pages will be created at 1st access, making the RSS
growing. if you free, nothing happens, or at least nothing is
different compared to not freeing. RSS can remain the same, and Vsize
will obviously remain the same. To get back memory to free list -
shrinking the RSS-, the standard memory allocation algorithm applies :
possibily paging out if free list is under vm_page_free_min or swapped
out if free list under vm_page_free_optimal and no process thread has
been runnable for the last 20/30 seconds.

- Process attributes include locking page in memory -(Real time
option). In this case all physical pages are created at malloc call,
not at access time. The strange thing is that pages will not be paged
out, but process can be swapped out !


This behavior is *very* efficient for standard malloc usage, and
-trade off- very poor for huge mallocs, followed by free(). Long
process inactivity which can lead to long swapout time of useless
data. Paging out these inconsistent pages is painful as well.

Emmanuel


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

end of thread, other threads:[~2002-08-28  8:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-08-27 10:37 Returning memory to system after a free Jan Sundberg
2002-08-28  2:26 ` Ian Cairns
2002-08-28  8:08   ` Emmanuel Choquet

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