public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Elladan <elladan@eskimo.com>
To: Karthikeyan Nathillvar <ntkarthik@ittc.ku.edu>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Process Memory Usage
Date: Fri, 12 Jul 2002 14:02:51 -0700	[thread overview]
Message-ID: <20020712210251.GA5803@eskimo.com> (raw)
In-Reply-To: <Pine.LNX.4.33.0207101811550.4626-100000@plato.ittc.ku.edu>

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/

      parent reply	other threads:[~2002-07-12 21:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020712210251.GA5803@eskimo.com \
    --to=elladan@eskimo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntkarthik@ittc.ku.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox