get ram usage using getrusage()

Oliver Fromme olli at lurza.secnetix.de
Mon Dec 20 08:09:19 UTC 2010


Claus Guttesen <kometen at gmail.com> wrote:
 > I'm trying to read how much ram an app is using reading

Could you phrase that question more precisely?
Note that "ram" and "memory" mean different things.
Are you interested in the VM mapped to the process?
Or do you want to know the amount of physical RAM only?
Do you want to include pages that are currently paged
to swap?  Do you want to include shared pages, or only
pages assigned exclusively to your process?  Do you
want to include memory that was free()ed but is still
mapped to your process?  And so on ...

It might be helpful to know *WHY* you are interested
in the app's RAM usage, in order to be able to give the
most appropriate advice.

 > http://unix.derkeiler.com/Mailing-Lists/FreeBSD/stable/2006-03/msg00246.html
 > from ru.ru_maxrss. While ru.ru_maxrss gives me used accurate ram-usage
 > when memory increases it doesn't immediately count down when memory is
 > released.

What do you mean by "released"?  If you mean free(), that
doesn't necessarily unmap pages from the process.  I think
that the current malloc() implementation uses madvise()
with MADV_FREE (but I'm not 100% sure).  But this is an
implementation detail that applications should not care
about.

 > So I tried to get a more accurate reading using this using
 > /usr/src/sys/kern/kern_clock.c as example:
 > 
 > struct thread *td;
 > td = curthread;
 > p = td->td_proc;
 > vm = p->p_vmspace;
 > rss = pgtok(vmspace_resident_count(vm));
 > 
 > Curthread is not defined, I searched google but can't find any references.

That's a piece of kernel source code.  It won't work in
user space.

 > But is this the proper way to get an apps memory usage?

I think asking for an app's "memory usage" is not proper in
the first place.  :-)

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"FreeBSD is Yoda, Linux is Luke Skywalker"
        -- Daniel C. Sobral


More information about the freebsd-stable mailing list