views:

1877

answers:

4

Hi, I am developing a .NET CF 3.5 network game. My issue is the app loads all the resources at first instance. However upon subsequent launches, the app gives me memory out of exception while loading resources especially sounds or big images.

Please guide me

A: 

How much memory is your application taking after loading all the resources ? On default settings I have been getting this error coming over cca 1.3 GB of private bytes (checking the task manager and the processes memory allocation).

Tomas Pajonk
1.3 GB? are u sure it is on .NET CF
Azlam
+1  A: 

I assume you're not attempting to lauch multiple instances of the game at a time. This sounds like memory is not being returned to the OS after your game shuts down. One simple way to determine if you have a leak is:

  1. Restart the device
  2. Check the memory usage
  3. Start your game, play it for a few minutes
  4. Close the game
  5. Wait a few minutes, then check the memory usage again

If you can get a few launches in before you run out of memory, you have a small leak. If you can only launch once before restarting the device, you have a BIG one. Garbage collection will only go so far. If you are making any calls to unmanaged code (Win32 or PInvoke calls that instantiate unmanaged objects,) then you need to be sure to release those resources when your game shuts down.

Dave Swersky
Dave,Thank you, the app was not properly shutting down (the task manager wasn't displaying it either). There was a connection thread which was executing! So i guess it made the app to go into the background,so it was not exiting properly. After aborting the thread, the problem is not occuring!
Azlam
A: 

Microsoft provides us with the CLR Profiler, a tool for analyzing the behavior of your managed application, which you can download here. It is CLR Profiler for .NET 2.0 but it will work with .NET 3.5 because CLR has not been changed.

Bug
A: 

Maybe part of the problem is that .NET CF does not handle Windows CE's memory model very well. (Large memory allocations fail with an OutOfMemory exception even if that much is available; I don't know the magic number where things start breaking...)

Here are some of the details: .Net Compact Framework Advanced Memory Management

The easiest way to work around it is to copy things to the ram disk (eg. \temp); plan B is to use something link Marshal.AllocHGlobal or P/Invoke VirtualAlloc (scary!) along with GCHandle.Pin