4

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

codingfreak
  • 4,467
  • 11
  • 48
  • 60

13 Answers13

6

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

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
3

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
  • 828
  • 5
  • 8
2

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
  • 4,803
  • 2
  • 23
  • 27
  • edgar there are some apps which on exit creates a great loss for businesses .... so what about in that case ?? – codingfreak Feb 18 '10 at 04:25
  • 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 Feb 18 '10 at 12:46
2

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
  • 233,326
  • 40
  • 323
  • 462
1

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
  • 1,552
  • 2
  • 18
  • 35
  • Ok it is something like for every timespan we will right down the old values and start back the profiler again ?? ... – codingfreak Feb 17 '10 at 13:21
  • @codingfreak: exactly thats it! – chrmue Feb 17 '10 at 14:41
  • 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 Feb 18 '10 at 04:31
  • @coding freak: If you are only interested in memory consumption shure! But profiling tells you a lot more – chrmue Feb 18 '10 at 08:40
0

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.
  • 3,814
  • 1
  • 23
  • 30
  • Seems it iss functionally similar to other memory debuggers, such as Valgrind. – codingfreak Feb 17 '10 at 10:41
  • IME the functionality of the Purify Windows & Unix versions are very close so it should be able to do this. – Timo Geusch Feb 17 '10 at 10:47
  • @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. Feb 17 '10 at 11:39
0

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
  • 7,746
  • 3
  • 38
  • 43
0

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.
  • 1,837
  • 13
  • 15
0

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
  • 8,656
  • 35
  • 45
  • 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 Feb 17 '10 at 13:19
  • 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 Feb 18 '10 at 13:10
0

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
  • 1,035
  • 2
  • 9
  • 14
0

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
  • 788
  • 5
  • 23
  • "Generates a report in the end" ... you mean if the app exits ?? If YES - then I got opensource tools like Valgrind ... – codingfreak Feb 18 '10 at 04:29
0

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.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
-1

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

zabulus
  • 2,373
  • 3
  • 15
  • 27