2002-07-10 23:15:35

by Karthikeyan Nathillvar

[permalink] [raw]
Subject: Process Memory Usage

Hi,

I want to find the memory usage of a particular process, to be precise
the percentage memory utilization. I need to find it through a program
other than "top".

1) I tried using getrusage() system call. But it is returning zero for
all values(like ru_maxrss, etc..) except ru_utime and ru_stime. I am using
2.2.18 kernel.

2) I tried to read from /proc/<pid>/statm file. But, the process memory
usage seems to be always in an increasing trend, even though lot of
freeing is going on inside. All the values, size, resident, are always
increasing.

Can anyone suggest me any other ways through which memory utilization
of a program can be obtained.

Thanking you in advance,
ntk.



2002-07-10 23:59:18

by Rik van Riel

[permalink] [raw]
Subject: Re: Process Memory Usage

On Wed, 10 Jul 2002, Karthikeyan Nathillvar wrote:

> 2) I tried to read from /proc/<pid>/statm file. But, the process memory
> usage seems to be always in an increasing trend, even though lot of
> freeing is going on inside. All the values, size, resident, are always
> increasing.

Remember that when you call free() glibc may decide to just
keep the memory for itself and not give it back to the OS,
so the memory is still in use by the process...

regards,

Rik
--
Bravely reimplemented by the knights who say "NIH".

http://www.surriel.com/ http://distro.conectiva.com/

2002-07-12 21:00:17

by Elladan

[permalink] [raw]
Subject: Re: Process Memory Usage

If you want to know heap memory utilization, probably the best way it to
shim on glibc's malloc:

volatile static int total = 0;

void *
malloc(size_t size)
{
void *ret = __libc_malloc(size + sizeof(size_t));
if(ret) {
lock();
total += size;
unlock();
*((size_t*)ret = size;
return (void*)((size_t*)ret+1);
} else {
return ret;
}
}

void
free(void *ptr)
{
if(ptr) {
lock();
total -= *((size_t*)ptr - 1);
unlock();
__libc_free((size_t*)ptr - 1);
}
}

define realloc, calloc too.

then,

LD_PRELOAD=./my_shim.so program.exe

This way, you can get accurate counts of heap activity. Or, you might
try to access the heap's internal data structures, if it has that
information.


If you only look at the kernel's numbers for size/rss/etc., these will
usually tend to always increase, because the kernel knows nothing of
your heap. It only knows about brk() space (increasing) and mapped
memory and such. The C library will manage the kernel's memory
allocation internally, usually by re-using space it previously allocated
instead of ever freeing it. Thus, the upward trend.

/proc/pid/status and statm are accurate, it's just that they only tell
you what the kernel's view of the process is. This may be very
different from the c library's view.

-J

On Wed, Jul 10, 2002 at 06:18:18PM -0500, Karthikeyan Nathillvar wrote:
> Hi,
>
> I want to find the memory usage of a particular process, to be precise
> the percentage memory utilization. I need to find it through a program
> other than "top".
>
> 1) I tried using getrusage() system call. But it is returning zero for
> all values(like ru_maxrss, etc..) except ru_utime and ru_stime. I am using
> 2.2.18 kernel.
>
> 2) I tried to read from /proc/<pid>/statm file. But, the process memory
> usage seems to be always in an increasing trend, even though lot of
> freeing is going on inside. All the values, size, resident, are always
> increasing.
>
> Can anyone suggest me any other ways through which memory utilization
> of a program can be obtained.
>
> Thanking you in advance,
> ntk.
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/