Re: Process Memory Usage

From: Elladan (elladan@eskimo.com)
Date: Fri Jul 12 2002 - 16:02:51 EST


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/
-
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/



This archive was generated by hypermail 2b29 : Mon Jul 15 2002 - 22:00:24 EST