Welcome to Soft32 Linux Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

System-API to get current process memory usage for C/C++ p..

 
   Soft32 Home -> Linux -> Development RSS
Next:  shmat fails problem in RedHat Linux 4  
Author Message
Matt

External


Since: Apr 02, 2006
Posts: 2



(Msg. 1) Posted: Wed Mar 15, 2006 12:48 pm
Post subject: System-API to get current process memory usage for C/C++ pgm?
Archived from groups: comp>unix>programmer, others (more info?)

What if any system calls exist to get the current process' system
memory usage (including all of the current process' threads) in a
C/C++ based program?

I'm trying to write unit tests to make sure that my memory allocation
processes (for Boost "smart pointers") are actually deallocating
correctly after thousands of dynamic allocations...and my presumption
is that a current-process-memory-usage "self check" will be very
useful for my self-running unit test.

I'd prefer cross-platform calls (be it POSIX, unix/linux, Boost C++,
etc) if I can get them, but I'll take a system specific all (for my
testing prototype) if I must.

Thanks for any help,
-Matt
--
Remove the "downwithspammers-" text to email me.
Back to top
Login to vote
Thomas Maier-Komor

External


Since: Mar 16, 2006
Posts: 1



(Msg. 2) Posted: Wed Mar 15, 2006 9:32 pm
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matt wrote:
> What if any system calls exist to get the current process' system
> memory usage (including all of the current process' threads) in a
> C/C++ based program?
>
> I'm trying to write unit tests to make sure that my memory allocation
> processes (for Boost "smart pointers") are actually deallocating
> correctly after thousands of dynamic allocations...and my presumption
> is that a current-process-memory-usage "self check" will be very
> useful for my self-running unit test.
>
> I'd prefer cross-platform calls (be it POSIX, unix/linux, Boost C++,
> etc) if I can get them, but I'll take a system specific all (for my
> testing prototype) if I must.
>
> Thanks for any help,
> -Matt
> --
> Remove the "downwithspammers-" text to email me.

Your assumption is incorrect concerning the virtual memory of a process.
A call to free() does not enforce memory mapped to a programs virtual
memory space to be unmapped. I.e. your program might do thousands of
mallocs and the exact number of frees and everything is fine, but your
program's virtual memory size is higher after the free as before the
mallocs.

I think you should take a look at the various tools available for
debugging memory related problems. Under Linux valgrind and efence might
get you started and under Solaris there is libumem (take a look at
umem_debug(3malloc)).

The only thing that could give you a portable solution is to write your
own mallocs and frees and use either sbrk() or mmap() to allocate new
address space. Before doing so, I suggest going to opensolaris.org and
taking a look at the libumem source code.

HTH,
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (SunOS)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEGM5R6U+hp8PKQZIRAqkrAKD0yPAIss7jgj2KZhpvhPDWzH+MbACg7wO7
ViL0h8N9Ktn2TlmdidsrUMM=
=pucF
-----END PGP SIGNATURE-----
Back to top
Login to vote
Gordon Burditt

External


Since: Mar 16, 2006
Posts: 1



(Msg. 3) Posted: Wed Mar 15, 2006 9:56 pm
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>What if any system calls exist to get the current process' system
>memory usage (including all of the current process' threads) in a
>C/C++ based program?

It's difficult to use this number for anything meaningful. In
particular, free() is unlikely to reduce it, since free()d memory
is usually (depending on the system) made available for reallocation
by this process itself, not given back to the OS.

>I'm trying to write unit tests to make sure that my memory allocation
>processes (for Boost "smart pointers") are actually deallocating
>correctly after thousands of dynamic allocations...and my presumption
>is that a current-process-memory-usage "self check" will be very
>useful for my self-running unit test.

A debugging version of malloc/free that tracks memory usage
would be much more helpful than something that tries to track *ALL*
memory usage.

>I'd prefer cross-platform calls (be it POSIX, unix/linux, Boost C++,
>etc) if I can get them, but I'll take a system specific all (for my
>testing prototype) if I must.

On some systems, you could get an approximation of this number from
sbrk(0). It's almost completely useless for tracking whether you forgot
to free() something. The part that isn't useless involves putting
whatever code in a loop and testing if the value continues to go up
indefinitely.

Gordon L. Burditt
Back to top
Login to vote
David Schwartz

External


Since: Jun 23, 2003
Posts: 89



(Msg. 4) Posted: Wed Mar 15, 2006 9:56 pm
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Gordon Burditt" <gordonb.75sek.RemoveThis@burditt.org> wrote in message
news:121hofa5b1erv65@corp.supernews.com...

> It's difficult to use this number for anything meaningful. In
> particular, free() is unlikely to reduce it, since free()d memory
> is usually (depending on the system) made available for reallocation
> by this process itself, not given back to the OS.

Note that this is *virtual* memory he is talking about here, not
physical memory.

DS
Back to top
Login to vote
Alan Woodland

External


Since: Nov 21, 2006
Posts: 1



(Msg. 5) Posted: Thu Mar 16, 2006 7:42 am
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Matt wrote:
> What if any system calls exist to get the current process' system
> memory usage (including all of the current process' threads) in a
> C/C++ based program?

There's an XPG/SVID extension that sounds like it might be what you're
looking for. GNU C library includes it:

http://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc...ml#Stat

Alan
Back to top
Login to vote
Ian Collins

External


Since: Jun 15, 2007
Posts: 5



(Msg. 6) Posted: Thu Mar 16, 2006 10:01 am
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Matt wrote:
> What if any system calls exist to get the current process' system
> memory usage (including all of the current process' threads) in a
> C/C++ based program?
>
> I'm trying to write unit tests to make sure that my memory allocation
> processes (for Boost "smart pointers") are actually deallocating
> correctly after thousands of dynamic allocations...and my presumption
> is that a current-process-memory-usage "self check" will be very
> useful for my self-running unit test.
>
> I'd prefer cross-platform calls (be it POSIX, unix/linux, Boost C++,
> etc) if I can get them, but I'll take a system specific all (for my
> testing prototype) if I must.
>
> Thanks for any help,
> -Matt
> --
> Remove the "downwithspammers-" text to email me.

I do this with my own operators new() and delete(). This assumes that
you don't use malloc in your C++.

If you use Solaris, the dbx debugger can do this for you on exit.

--
Ian Collins.
Back to top
Login to vote
Gordon Burditt

External


Since: Mar 16, 2006
Posts: 1



(Msg. 7) Posted: Thu Mar 16, 2006 1:20 pm
Post subject: Re: System-API to get current process memory usage for C/C++ pgm? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> It's difficult to use this number for anything meaningful. In
>> particular, free() is unlikely to reduce it, since free()d memory
>> is usually (depending on the system) made available for reallocation
>> by this process itself, not given back to the OS.
>
> Note that this is *virtual* memory he is talking about here, not
>physical memory.

And so am I. free() is unlikely to give back memory, physical or
virtual, to the OS.

Some of the reasons include (1) parts of the page may still be in
use, (2) an OS that uses sbrk() can only return the last memory
allocated, (3) mmap() aligns stuff by pages and the only really
practical way to be able to release memory is to use one mmap() per
request, but it wastes so much memory for small (e.g. less than 4
pages worth) requests it's not practical, and (4) hanging on to the
memory in the process for later re-use is considered an optimization
which is usually effective but can be pathological in corner cases.

Gordon L. Burditt
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Mapping big chunk of virtual memory / file to small physic.. - Sub: Mapping big chunk of virtual memory / file to small physical memory in kernel 2.4 Message: Hi, I am fixed in a....

memory - Hi , Whenever i read data from disk , i see same amount of data is pagedin : sar -B: 08:42:48 AM pgpgin/s pgpgout/s ...

X Server eats up my memory - Hi, When I start my X with 'startxfce4' (http://www.xfce.org), then it uses up to 30 MB of RAM. Over the time this..

addressing memory in 32b - I just want to know what how much memory is really available by an application on a linux 2.6 with an intel 32b..

Reserving a chunk of memory exclusively for virtual mappin.. - Sub: Reserving a chunk of memory exclusively for virtual mapping a file Message: Hi, Can any one provide me some..

source code for shutdown - Hi, Can anyone tell me where to get the source code for the command shutdown. I would like to port it to my Agenda..
       Soft32 Home -> Linux -> Development All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Categories:
 Windows
  Linux
 Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]