LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-11-2012, 04:20 AM   #1
simopal6
Member
 
Registered: Jun 2006
Location: Italy
Distribution: Slackware 13.1
Posts: 190

Rep: Reputation: 30
C++, keep track of memory used by a certain class


Hi everyone,
I have to evaluate the performance of an algorithm (implemented as a C++ class), in terms of execution times and memory usage. I can easily handle the execution times, however I've got a few problems with the memory used by the class.
Let's suppose I have a getRAMUsage() function, which returns the amount of resident memory used by the whole program. Since there are also other components used in the program, and some of them need a non-negligible amount of memory, my problem is to find out how much memory ONLY MY COMPONENT needs.
So, here's the structure of my program so far:
Code:
// Initialize my component, and see how much more memory is allocated
long allocation_ram = getRAMUsage();
MyComponent *component = new MyComponent();
allocation_ram = getRAMUsage() - allocation_ram;
long total_ram_used = allocation_ram;
// Enter the processing loop
while(!stop_condition)
{
    // Do some stuff...
    // Run component and see how much more memory it allocates at each cycle
    long cycle_ram = getRAMUsage();
    component->doSomething();
    cycle_ram = getRAMUsage() - cycle_ram;
    total_ram_used += allocation_ram;
    // Do some other stuff...
}
So, in the end, total_ram_used should indicate the amount of RAM allocated by my class during execution.
In my case, cycle_ram should be 0 all the time, since it allocates some memory, works with it, and frees it when it's done. This basically works (I mean the computation of the RAM usage by the component), but sometimes cycle_ram is a few MB, although the total RAM usage by the program (checked with 'top') doesn't change.
My explanation to this is that the runtime library might deallocate some memory allocated by doSomething() after the second call to getRAMUsage(). Does this make sense? Or maybe my approach is completely wrong? :-)

Thanks for your help!
 
Old 02-11-2012, 07:29 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
I would look at keeping a running total as a static and when you allocate an object of your class then increment that total by the sizeof your object. When you free decrement the total. You will also need a static that holds the maximum amount allocated because, assuming no memory leak once your program has finished the allocated total should be zero.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does making a class a 'friend' make the class' member classes 'friends'? Jusctsch Programming 7 11-17-2011 07:58 PM
LXer: Track Me! Just Track Me, GNOME Project! LXer Syndicated Linux News 0 03-02-2011 01:41 AM
CPP: Question about memory consumption of many vitural functions in a class of mine. RHLinuxGUY Programming 3 04-30-2007 11:01 PM
PHP class extends.... with no memory? candela Programming 3 05-03-2006 07:03 AM
Allocate memory in class contructor Ephracis Programming 3 01-13-2005 10:50 AM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration