public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Process Memory Usage
@ 2002-07-10 23:18 Karthikeyan Nathillvar
  2002-07-11  0:01 ` Rik van Riel
  2002-07-12 21:02 ` Elladan
  0 siblings, 2 replies; 3+ messages in thread
From: Karthikeyan Nathillvar @ 2002-07-10 23:18 UTC (permalink / raw)
  To: linux-kernel

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.



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

* Re: Process Memory Usage
  2002-07-10 23:18 Process Memory Usage Karthikeyan Nathillvar
@ 2002-07-11  0:01 ` Rik van Riel
  2002-07-12 21:02 ` Elladan
  1 sibling, 0 replies; 3+ messages in thread
From: Rik van Riel @ 2002-07-11  0:01 UTC (permalink / raw)
  To: Karthikeyan Nathillvar; +Cc: linux-kernel

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/


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

* Re: Process Memory Usage
  2002-07-10 23:18 Process Memory Usage Karthikeyan Nathillvar
  2002-07-11  0:01 ` Rik van Riel
@ 2002-07-12 21:02 ` Elladan
  1 sibling, 0 replies; 3+ messages in thread
From: Elladan @ 2002-07-12 21:02 UTC (permalink / raw)
  To: Karthikeyan Nathillvar; +Cc: linux-kernel

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 majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

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

end of thread, other threads:[~2002-07-12 21:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-10 23:18 Process Memory Usage Karthikeyan Nathillvar
2002-07-11  0:01 ` Rik van Riel
2002-07-12 21:02 ` Elladan

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