Retrieve Heap memory size and its usage statistics etc...? - c++

Lets say I open some application or process. Did some work with that. Now I closed it.
Need to know whether this application caused any memory leak.
i.e used up some heap memory and not cleared it properly.
Can I get this statistics some how? I'm using Visual Studio (for development) under Windows OS.
Even I would be interested in knowing this information for any 3rd party application.

When an application closes all resources are automatically released by Windows.
A quick & dirty tool to get an indication for memory/resource-leaks inside an application is Perfmon.
The actions executed by an application, can cause other processes to use more memory. SQL Server can make its cache size bigger, maybe you have opened Word or Explorer, the Windows Search engine might kick in because you saved some file. The virus scanner can be more active, etc.....

Have a look at CrtSetDbgFlag:
http://msdn.microsoft.com/en-us/library/5at7yxcs(v=VS.100).aspx

Related

Software for checking memory leakage in exe file

I have an application written in VC++ Windows form applications that interacts with various hardware such as A/D cards, GPIB, D/A etc. My customer ran the application on-site, and found that the application is crashing after few seconds. I asked him to monitor memory growth through task manager, and I found that indeed , the memory was growing. So it looks like some problem of memory leakage. Now I want to find where exactly in my code , I am not correctly freeing/allocating memory., but I do not have the access to the onsite PC. I have to do this on my PC which is not having those hardware A/D etc. Is there any software that can accept my exe, and point out the name of functions/code line which is causing the problem, without actually ecxuting my exe?
My exe would not run since I do not have those hardware.
I used Smartbear QATime for this tasks. It is a profiler which can also profile heap allocations. In the report, you can get a list of objects which are still alive and also the line where they have been created.

ADsOpenObject() returns -2147024882 (0x8007000E) -> OUT_OF_MEMORY

I have a C++ DLL that is used for authentication that gets loaded by a Windows service for every login.
In that DLL I use the Windows ADSI function ADsOpenObject() to get a user object from Active Directory.
HRESULT hr = ADsOpenObject(L"LDAP://rootDSE",
L"username",
L"password",
m_dwADSFlags,
IID_IDirectorySearch,
(void**)&m_DSSearch);
Generally this works since years. But currently I get the error code
-2147024882 (0x8007000E)
which is OUT_OF_MEMORY. When I restart the service that is using my DLL then it runs fine for weeks but then the errors start occuring.
Now I can't find what is out of memory. The task scheduler looks fine and free memory is plenty.
What can I do to fix that?
which is OUT_OF_MEMORY.
It is E_OUTOFMEMORY, a COM error code. The description isn't very helpful, this error code tends to be returned for any "out of resources" errors by Microsoft code, not just memory. Could by an internal limit being reached, could be a winapi call that fails.
And it is not necessarily limited to the direct software involved. A mishaving device driver that leaks kernel pool memory can be the indirect source of the mishap for example.
You'll be lucky if you can find something back in the Application event log, look both in the machine that reports the error as well as the domain server. Task Manager might give a clue, add the Handles, GDI Objects, USER Object, Commit size, Page pool and NP Pool columns. Pretty hard to give specific advice beyond this. It is no doubt a leak, quacks loudly like one when you have to restart a machine periodically to recover. Good luck hunting it down.
Are you calling Release on m_DSSearch? Also if you are searching you need to call CloseSearchHandle or AbandonSearch. If you are not doing either of those, you could be slowly leaking memory.
Your process could fragment the heap to a point so that ADsOpenObject can't find a consecutive piece of memory that is large enough.
You can use VMMap to profile your memory usage:
http://technet.microsoft.com/en-us/sysinternals/dd535533
You could try enabling low fragmentation heap:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366750(v=vs.85).aspx
If it is now starting to appear and it wasn't earlier, I would suppose one of two possibilities: it has to do with the time (more specifically the year, a milleniumbug of some kind). Another option would be a problem with 32/64 bit architecture.
Now mind that I don't program C++. But I do know a bit about MS errors (I have worked on a MS helpdesk...)

Redirect APPCRASH Dumps (Or turn them off)

I have an application (didn't write it) that is producing APPCRASH dumps in C:\Windows\SysWOW64. The application while dumping is crippled, but operating at bare minimum capacity to not lose data. The issue is that these dumps are so large that the system is spending most of it's time writing these and the application is falling far behind in processing and will start losing data soon.
The plan is to either entirely disable it, or mount it to a RAM drive and purge them as soon as they hit the RAM drive.
Now I've looked into using this key:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
But all it does is generate a second dump now instead of redirect the original.
The dump is named:
dump-2013_03_31-15_23_55_772.dmp
This is generally the realm of developers on Windows (with stuff like C/C++) so I'd like to hit them up, don't think ServerFault could get me any answers on this.
Additionally: It's not cycling dump files (they'll fill the 20GBs left on the hard drive), so I'm not sure if this is Windows behavior or custom code in the app (if it is... ick!).
To write a DumpFile, an app has to call the function "MiniDumpWriteDump" so this is not a behavior of the system or something you can control, it is application driven. If it dumps on crashes, it uses "SetUnhandledExceptionFilter" to set its own handling routine, before(!) the OS takes over. Unfortunately I didn't found a way to overwrite this handler from an other process, so the only hope left is, that there is a register entry for the app switching the behavior or change the path (as my applications have it for exactly the reason you describe).

What is a normal Memory Usage for a request in Coldfsuion

Looking at Coldfusion's Server Monitor, I see many requests with averages from 100KB to 700KB average memory usage. This seems high to me, is it?
If it is high, it would explain why the server seems to love to eat up RAM and never let it go. What would be a good way of finding out why the memory usage is so high. There is nothing that I can think of that would be using this much memory. Are there any good ways of debugging Coldfusion requests?
We've had good luck with SeeFusion and simply using Jconsole on the servers to monitor memory and threads.
One thing we did find is that during the time in which the data is being rendered to be returned to the user the memory usage was highest. Once the form was displayed memory dropped off significantly. Make sure garbage collection is set properly, as well.
Are you actually getting 'out of memory' exceptions? Or 'out of stack space' errors? Check the logs carefully as I had experience with CF restarted spontaneaouly before - check the server.log files.
If the answer is yes, then - as mentioned - you can use a tool like JConsole to monitor memory usage. If you see that memory goes up and up prior to these exceptions then you need to find out where your memory leak is. The best way to do this is to get a dump of the JVM at that time and use a meomry analysis tool. I documented our experience with this.
If there are no exceptions etc. then be aware than JRun does use a lot of memory. Typically between 500MB and 1.5GB on our Win32 servers even if the CF Admin setting for the JVM heap size is 1024MB. JRun and is running on top of Java, running an application server so this will be high enough. What you need to watch out for is an ever-increasing use of memory using JConsole.
As for GC tuning - only mess with these settings if you really have a problem. It's complicated and will result in server instability if done incorrectly or without understanding of how Java GC works. You can get the basics here, and then you can pass the settings (e.g. -XX:MaxPermSize=192m) as JVM argument in CF admin.
Getting to the bottom of memory issues can be challenging and time consuming so best of luck!
Sounds like a memory leak. You want to generate a JVM heap dump - a snapshot of JVM memory. Easiest way to do so is with jvisualvm. Then analyze that snapshot using the Leaks Report in Eclipse Memory Analyzer. Marc Escher has a blog entry on this topic.
Steps to Setup a JVM JMX Port for getting a ColdFusion Heap Dump
Edit jvm.config and add to java.args: -Dcom.sun.management.jmxremote.port=3333 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true
Copy \ColdFusion9\runtime\jre\lib\management\jmxremote.access.template -> jxmremote.access
Edit jxmremote.access add "controlRole changeIt" to the end of the file replacing "changeIt" with password of your choice
In Windows Explorer > jxmremote.access > Right Click > Properties > Security > Advanced
Owner > Administrators
Permissions > Uncheck "Allow inheritable...." > Dialog Click "Copy"
Add "Administrators" with Full Control
Remove all other Permission entries
Click "OK"
Restart ColdFusion
Generating a Heap Dump
on ColdFusion box install the JDK
run jvisualvm from jdk-install-dir/bin/jvisualvm
open a JMX connection (e.g. localhost:3333)
on the "monitor" tab click the Heap Dump button and save the result by right clicking its node
take this file and open in Eclipse Memory Analyzer and run the Leaks Report
dig through it, use the Eclipse help to understand how to evaluate
I've used this approach to find a number of memory leaks in ColdFusion code.

How do you get how much memory a program uses?

I have two programs, one in C++, the other in assembler. I want to compare how much memory they use when running respectively. How can I do this?
I am doing the testing on Windows, but I also would like to know how to do it on Linux.
On Linux, try valgrind. It's an amazing tool with too many features for mere mortals to totally comprehend. Have a look at valgrind's massif.
Run the program in one shell. Open another shell and run 'top' command. it will list running processes and home much memory they consume. you can, i guess, poll /proc/yourprocessid/stat to see how much memory it is using over time.
On Windows you can use Performance Monitor.
Performance monitor usage
Start Performance Monitor from Start menu/ Administrative Tools/ Performance
If you want to start the logging:
Select performance log and alert>Current Log option in the left side of the browser.
Select New Log Settings.
Give an apropriate name to the log e.g. performance_Server for Server
It will prompt you one menu. In “general” tabs click on the add button and select the process you want monitor. (Change the performance object to process, for “select counters from list” select “private bytes”, for “select instances from list”, select the process you want to monitor.) After that click on Add and close. Now change the interval as per test case requirement. Now go to “log files” tab change the log file type to either csv or tsv format. Now apply and press OK.
If you want to start/stop the logging:
Select the particular log you want to start and stop.
In toolbar above you will see start and stop button.
If you want to check the content of a log file:
Click Options/Data From…
Select the log file to be viewed, click OK
Go to the chart screen (View/Chart)
Click Edit/Add to chart
Add the required items to the chart. (In case the memory leakage is to be checked, then you need to view the PrivateBytes of the processes and the _Total of them)
Read the values from the chart (Min and Max values are displayed at the bottom of the chart)
If you want to monitor network transfer:
Display the chart screen (View/Chart)
Click Edit/Add to log, and select the items Network Interface\Bytes Sent If you set it in dl
Or Network Inerface\ Bytes Received if you set it in the CRS-PC+
Click Done
Monitor memory usage:
In menu Start/Programs/Administrative Tools/ start the program Performance Monitor
Click on the button to open the window that adds processes
Fill the fields as follows:
Object: Process
Counter: Private Bytes
Instance: a process whose Memory occupation need to be displayed
Click on Add button
Repeat the last two steps for every process the memory needs to be displayed
Close the window that adds processes
On the bottom of the Performance Monitor window, there is the list of the processes previously selected.
How to use the logged data
Now open the file Perfmon_.csv or Perfmon_.tsv using WordPad or Excel.
If you have opened the file using Excel, then using the option Save As, save the file in the Microsoft Excel format.
The Windows Task manager can show you the memory usage of each process. I guess you could use Valgrind instead, but I don't see the point in that. On Linux, use Valgrind or ps.
On Windows you can use the GetProcessMemoryInfo Function.
Here is an example on how to use it:
Collecting Memory Usage Information For a Process
Depends on your operating system - you would expect to have tools to tell you the memory consumed when the apps are running.
Trying to infer the answer by inspecting the code would be very difficult, run the apps, use your platform's diagnoistics.
Depending on the size of the programs, this could be nearly unfeasible.
If they are not very large, then you can see how much memory they allocate; for instance, an int would take up 4 bytes, a char would take 1 byte, etc. Assembly is very transparent in how much memory it is using, even on an x86 machine. Cpp is nearly as transparent, as long as you faithfully track object creation and memory destruction/allocation.
If the program is huge, you'll need to use specific tools for tracking/profiling memory use, like GlowCode (http://www.glowcode.com/summary.htm).
On Windows, you can use Microsoft's Performance Monitor to do it. Start, Run, "perfmon". This tool will report on all sorts of statistics about processes, and provide graphs for you. In general, you're going to be interested in reporting on the "Private working set". This will tell you how much memory your process has reserved for its own use.
If you want to just get your heap usage, and you want to do it programatically, you should look into the CRT Debug Heap.
I'm not sure about Linux though, sorry.
On Windows, I have found Address Space Monitor very useful, especially for looking at how fragmented your memory is.