views:

288

answers:

13

Hi

I have a small doubt regarding profiling applications which never exit until we manually reboot the machine.

I used tools like valgrind which talks about memory leaks or bloating of any application which exits after sometime.

But is there any tool which can be used to tell about memory consumption, bloating, overhead created by the application at various stages if possible?

NOTE: I am more intrested to know about apps which dont exit ... If an app exits I can use tools like valgrind ..

A: 

Work of program that profiling memory leaks is based on detecting memory that was freed by OS not by program.

zabulus
I didn't get you zabulus
codingfreak
+7  A: 

I'd consider adding a graceful exit from the program.

Didier Trosset
But what if the program never exits and keeps on running for days ???
codingfreak
@codingfreak Don't write applications like that - all apps, even servers, should have a means of being closed down in a controlled manner.
anon
nearly every embedded application never exits
chrmue
Yeah u r right @chrume
codingfreak
A: 

Rational Purify can do that, at least on windows. There seem to be a linux version but I don't know if it can do the same.

f4
Seems it iss functionally similar to other memory debuggers, such as Valgrind.
codingfreak
Timo Geusch
@codingfreak with purify you can start you app, wait for it to become stable, scan for memory leaks (there is likely to be a lot at this stage), wait a bit more and scan only *new* memory leaks
f4
A: 

Some tools allow you to force a memory analysis at any point during the program's execution. This method is not as reliable as checking on exit, but it gives you a starting point.

Here's a Windows example using LeakDiag.

rpg
hmmm i am looking something in opensource and Linux oriented
codingfreak
A: 

Have you tried GNU Gprof?

Note that in this document, "cc" and "gcc" are interchangeable. ("cc" is assumed as an alias for "gcc.") http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html

Max E.
You can do better than **gprof** : http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343
Mike Dunlavey
A: 

Your question reads as if you were looking for top. It nicely displays (among other things) the current memory consumption of all running processes. (Limited to one page in the terminal.) On Linux, hit “M” to sort by memory usage. The man page shows more options for sorting and filtering.

Christopher Creutzig
yeah top might be helpful in determining the total space usage of a application ... but we cant detect how much of the memory is a bloated or a leaked one right ??
codingfreak
Right. And without some garbage collection-like system, there is no way to detect leaked memory while the program is running – how is leaked memory different from active, but currently dormant memory? Distinguishing between bloat and memory that is actually used cannot be automated anyway. At least not fully, and I'd be somewhat surprised by usefully working partial solutions.
Christopher Creutzig
A: 

I have used rational purify API's to check incremental leaks. Haven't used the API's in linux. I found the VALGRIND_DO_LEAK_CHECK option in Valgrind User Manual, I think this would suffice your requirement.

Andy
But valgrind requires the app to be exited right ??
codingfreak
You need to include memcheck.h in your program and use VALGRIND_DO_LEAK_CHECK. I haven't used this, please check.
Andy
A: 

You can use GNU gprof, but it has also the problem that it requires an exit of the program. You can overcom this by calling internal functions of gprof. (see below) It may be a real "dirty" hack, depending on the version of gcc and, and, and,... but it works.


#include "sys/gmon.h"


extern "C" //the internal functions and vars of gprof
{
  void moncontrol (int mode);
  void monstartup (unsigned long lowpc, unsigned long highpc);
  void _mcleanup (void);
  extern void _start(void), etext(void);
  extern int __libc_enable_secure;
}

// call this whenever you want to write profiling information to file
void WriteProfilingInformation(char* Name)
{
  setenv("GMON_OUT_PREFIX",Name,1);  // set file name

  int old = __libc_enable_secure; // save old value
  __libc_enable_secure = 0;       // has to be zero to change profile file name
  _mcleanup();
  __libc_enable_secure = old;     // reset to old value

  monstartup(lowpc, highpc);      // restart profiler
  moncontrol(1);                  // enable profiler
}
chrmue
Ok it is something like for every timespan we will right down the old values and start back the profiler again ?? ...
codingfreak
@codingfreak: exactly thats it!
chrmue
Is it an efficent way to do .. ??? By timespan we might be knowing the memory consumption is increased or decreased .. even with the help of top we can do the same right ?? sorry If I am wrong I am just saying
codingfreak
@coding freak: If you are only interested in memory consumption shure! But profiling tells you a lot more
chrmue
A: 

For windows, DebugDiag does that. Generates a report in the end with probable memory leaks. Also has memory pressure analysis. And it's available for free @ microsoft. Download it from here

Samrat Patil
"Generates a report in the end" ... you mean if the app exits ?? If YES - then I got opensource tools like Valgrind ...
codingfreak
+1  A: 

dtrosset's point is well put but apparently misunderstood. Add a means to terminate the program so you can perform a clean analysis. This can be something as simple as adding a signal handler for SIGUSR1, for example, that terminates the program at a point in time you decide. There are a variety of methods at your disposal depending on your OS.

There's a big difference between an application which never exits (embedded, daemons, etc) and one that cannot be exited. The prior is normal, the latter is bad design.

If anything, that application can be forcibly aborted (SIGKILL on *nix, terminate on win32) and you'd get your analysis. That method doesn't give your application the opportunity to clean up before it's destroyed so there will be very likely be retained memory reported.

brlcad
A: 

You need stackshots. Either use pstack or lsstack, or just run it under a debugger or IDE and pause it (Ctrl-C) at random. It will not tell you about memory leaks, but it will give you a good idea of how the time is being used and why.

If time is being used because of memory leaks, you will see a good percent of samples ending in memory management routines. If they are in malloc or new, higher up the stack you will see what objects are being allocated and why, and you can consider how to do that less often.

Mike Dunlavey
+2  A: 

Profiling is intrusive, so you don't want to deploy the app with the profiler attached, anyway. Therefore, include some #ifdef PROFILE_MODE-code that exits the app after an appropriate amount of time. Compile with -DPROFLILE_MODE, profile. Deploy without PROFILE_MODE.

edgar.holleis
edgar there are some apps which on exit creates a great loss for businesses .... so what about in that case ??
codingfreak
In that case you still wouldn't run the app with attached profiler. Safety critical apps usually make use of redundancy, business critical apps usually employ monitoring, proactive consistency checking and ample proofing / testing prior to deloyment. What are you looking for?
edgar.holleis
+1  A: 

Modify your program slightly so that you can request a Valgrind leak check at any point - when the command to do that is recieved, your program should use VALGRIND_DO_LEAK_CHECK from memcheck.h (this will have no effect if the program isn't running under Valgrind).

caf