TomOW

Hi,

I am having some issues with Out Of Memory exceptions in my WM6 project written in C#. I think the problem is related to my use of the CameraCaptureDialog. Specifically, at seemingly random times, it just says it doesn't have enough memory to capture an image. Sometimes this happens on the first photo I try to take. At other times, I can capture many images before I get it.

I assume my program must have a memory leak somewhere, and I'm trying to track it down. I've been going to Start, Settings, Memory, to check the current memory status. On the Main tab, I see something like this:

Program

Total: 25.45 MB
In use: 22.91MB
Free: 2.54 MB

At various times when my program is running, I come back to this screen, and the last two numbers vary as expected. It seems to be very much tied to how many forms are open at the time.

Anyway, I want to be able to calculate these 3 numbers programmatically, so that I can log the amount of memory free at various point in my application, to try to figure out where the memory leak might be. The only thing I can find is the GC.GetTotalMemory() function. The strange thing is that the number returned by this function doesn't seem to have anything to do with the In use or Free numbers I see on the Settings page. For example, at the time that the above numbers were shown on the Setting screen, GC.GetTotalMemory() returned 1.72 MB.

So my question is: How can I get the Total, In use, and Free memory numbers programmatically, the way the Settings/Memory screen shows them

(By the way, if anyone has suggestions for the easiest way to track down memory leaks, I sure would like to hear them!)

Thanks in advance.

Tom


Re: .NET Compact Framework Program Memory

Beerka

[DllImport("coredll.dll")]

public static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);

[DllImport("coredll.dll")]

public static extern int GetSystemMemoryDivision(

ref UInt32 lpdwStorePages,

ref UInt32 lpdwRamPages,

ref UInt32 lpdwPageSize

);

public struct MEMORYSTATUS

{

public UInt32 dwLength;

public UInt32 dwMemoryLoad;

public UInt32 dwTotalPhys;

public UInt32 dwAvailPhys;

public UInt32 dwTotalPageFile;

public UInt32 dwAvailPageFile;

public UInt32 dwTotalVirtual;

public UInt32 dwAvailVirtual;

}

and usage:

MEMORYSTATUS memorystatus;

GlobalMemoryStatus(ref memorystatus);

After that you can log the memory status before and after every method or memory critical operation.

(u can use any standard logger or log4net...)

And one more thing you can monitor the memory changes with Remote Performance Monitor:

http://blogs.msdn.com/stevenpr/archive/2006/04/17/577636.aspx

I dont know your program but if you use bitmaps for capture be sure u dispose the bitmaps when u dont need them.





Re: .NET Compact Framework Program Memory

TomOW

Thanks so much for the reply, it was very helpful!

I'm trying to also get the Remote Performance Monitor to work. Since I'm running on a WM5 device (Treo 700w), I need to use RAPIConfig. However, when I try to run it, I get an "Access is Denied" error. Any idea what I can do about it

I tried following the "I get "Access Denied". How do I get around this " instructions at:

http://www.breaksoft.com/Blog/Posts/2005/8/Mobile_Registry_Editor_FAQ.aspx

but I also get "Access is Denied" when I try to run the EnableRAPI.bat file. Any ideas how I can get the Remote Performance Monitor to work on my Treo

(I did get everything set up to monitor in the emulator, but when I click on "Connect", though it does launch the application, it says "CreateStream FAILED: Exception from HRESULT 0x89731302" at the bottom and never connects.)

Thanks again!

Tom




Re: .NET Compact Framework Program Memory

Ilya Tumanov

See this, item 16 and 20.




Re: .NET Compact Framework Program Memory

jaczura

Hello!

memorystatus.dwAvailPhys is for the free program memory.

But I cannot find how to retrieve storage memory and Storage Card Memory status.

Could you help me

Thank you!





Re: .NET Compact Framework Program Memory

MarkIsMobile

The best way is by using the new Remove Performance Monitor that is available in VS 2008 ... it works with mobile unlike 2003 or the original 2005. This can also be accomplished by using the tool in SP2 of the Compact Framework (v2 of course).

The first thing to check is if you notice your application getting progressively slower as you open / close forms, then make sure you are correctly calling the Dispose() method on the form when you are done using it. Everyone thinks that the GC in .NET will take care of cleaning up after them ..... this is true, but only to a point. The GC will automatically take care of the "managed" portion of an object, but any native objects (which Windows CE / Mobile uses extensively under the covers with all GUI widgets) can remain in memory if not taken care of properly. By calling Dispose() you'll help ensure the native components are removed as well as the managed pieces too.

A good rule of thumb is any object which exposes a Dispose() method you should call when you're done with it ..... Forms, Camera objects, etc., etc., etc ...... unless you're certain this method will be called for you from somewhere else. Even then, if the call will take a long time to occur, you still may elect to do this yourself.