<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Xref: feenix.metronet.com comp.sys.sun.admin:6695
Newsgroups: comp.sys.sun.admin
Path: feenix.metronet.com!news.ecn.bgu.edu!wupost!howland.reston.ans.net!xlink.net!rz.uni-karlsruhe.de!stepsun.uni-kl.de!uklirb!bogner.informatik.uni-kl.de!lott
From: lott@bogner.informatik.uni-kl.de (Christopher Lott)
Subject: Re: How much "free memory" is too little? 
Message-ID: &lt;1993Jun3.081729.24460@uklirb.informatik.uni-kl.de&gt;
Sender: news@uklirb.informatik.uni-kl.de (Unix-News-System)
Nntp-Posting-Host: bogner.informatik.uni-kl.de
Organization: Universitaet Kaiserslautern FB Informatik
References: &lt;1993May26.175329.19359@dragon.acadiau.ca&gt; &lt;BEAULIEU.93May28142631@winston.bose.com&gt; &lt;1993Jun1.013531.20168@newsgate.sps.mot.com&gt;
Date: Thu, 3 Jun 1993 08:17:29 GMT
Lines: 70

In article &lt;baz&gt; mario@wdc.sps.mot.com (Mario Nigrovic) writes:
&gt;To discover how much physical memory is used, look at the RSS fields
&gt;in PS.  You'll find a large hole between (physical - RSS_total) and
&gt;(free_memory).  The former is the *real* available memory.


Here is a variant on that idea.  This perl script totals up the SIZE
fields from top.  The top program gives text + stack + data in that
column, which I felt was the data that I want.  According to the man
page for ps, it shows in its SIZE column just stack + data and the
resident set size in RSS.  

chris...

--snip--
#!/usr/local/bin/perl
# a script to total the SIZE fields from top
# permission to use, abuse, modify, fold, spindle, and mutilate granted
# please send improvements to me, I'm interested in seeing them.
#
# cml 920603, lott@informatik.uni-kl.de

open(TOP, "top -buSI max | ")  || die "Can't open a pipe?\n";

# skip the first 3 lines: lastpid/process count/blank
for ($i = 0; $i &lt; 3 &amp;&amp; &lt;TOP&gt;; $i++)  { }

# grab the content of the 4th line, which looks like this:
# Memory: 29136K available, 23976K in use, 5160K free, 1540K locked
#         ^^^^^^            ^^^^^^
$_ = &lt;TOP&gt;;
chop;
($m, $av, $a, $in, $rest) = split(' ', $_, 5);
$available = int($av);
$inuse = int($in);

# skip lines 5-7
for ($i = 4; $i &lt; 7 &amp;&amp; &lt;TOP&gt;; $i++)  { }

# now process the lines from top
while (&lt;TOP&gt;) {
    # remove trailing newline
    chop;
    # split line at whitespace; just keep the first 5 fields
    ($pid, $uid, $pri, $nice, $size, $rest) = split(' ', $_, 6);
    # extract just the numerical part
    $numsize = int($size);
    # print $size, " ", $numsize, "\n";
    # add up
    $total = $total + $numsize;
}

close(TOP);

# sanity check
if ($available == 0 || $inuse == 0 || $total == 0) {
   print "Detected nonsense from top:\n";
   printf("available = %d, inuse = %d, total = %d\n", 
		     $available, $inuse, $total);
   exit 1;
}

printf("O/S reports  %d of %d, or %.0f%% in use\n", 
	    $inuse, $available, ($inuse/$available) * 100);
printf("Top computes %d of %d, or %.0f%% in use\n", 
	    $total, $available, ($total/$available) * 100);
--snip--
-- 
"Christopher Lott   lott@informatik.uni-kl.de   +49 (631) 205-3334, -3331 Fax"
"Post: FB Informatik - Bau 57, Universitaet KL, 67653 Kaiserslautern, Germany"
</pre></body></html>