[dtrace-discuss] Getting total memory allocated so far

James Carlson
Thu, 03 Jan 2008 13:09:27 -0000

Lally Singh writes:
> @James: I saw that, but could I use it on a running program without
> stopping it?

You can attach to the process using "mdb -p <pid>" or use
"gcore" to
get a core-file snapshot of the running process.

Something like:

	echo ::umastat | mdb -p `pgrep myapp`

> I''d like to monitor memory usage without interfering with my other
> measurements.  Hence, I''d love to know something close to a global
var
> (which''d be great, malloc() already has to do some locking (for
normal
> malloc, haven''t seen umem), so why not?) or some way to quickly
> assemble a running total.  Right now all I''ve got is a pid
provider
> probe for malloc & free that reads the headers on the memory blocks
> they use.

If all you want is a "quick" and non-intrusive answer, then
I''d think
that ps(1) or vmstat(1M) would be a good tool.  Using
mallinfo(3MALLOC) functions would provide more details, at the expense
of getting the application involved.

Dtrace can do it, but you''d be tracing every single malloc, calloc,
free, memalign, realloc, and valloc call to get the numbers, and then
keeping associative arrays to monitor allocations.  It sounds somewhat
expensive to me.

Still another approach would be to write your own interposition
library that keeps the statistics you want.  I''m not sure why
instantaneous memory allocation would be an interesting value, but it
sounds like it''d be doable that way.

Yet another would be to modify malloc/free in libc.  You have the
source ...

--
James Carlson, Solaris Networking              <james.d.carlson at
sun.com>
Sun Microsystems / 35 Network Drive        71.232W   Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757   42.496N   Fax +1 781 442 1677