Question

I'm currently working on an application which runs on Windows Mobile 6.1 (not WP). I built an application which synchronizes data from a remote server multiple times a day. But somehow it looks like this data is "remembered" after finishing. Task Manager shows that about 3MB is used at a regular start of the application, which increases with about 2MB everytime I run the synchronization. After multiple times I get a warning of the memory usage and I have to reset the device or restart the program.

What I'm looking for is some way to clear data after synchronization, a kind of garbage collector. In (regular) C# I've found Collect(), but I can't get this working in C# mobile.

Below is my code, which is working correctly, except at a certain point I get the message "Geheugentekort" ("Memory shortage").

Probably after the for{} code, I have to empty variables like doc, root, and the XmlNodeList, but the question is how...

My device: Pidion BIP-5000 OS: Windows Mobile 6.1

XmlDocument doc = new XmlDocument();
doc.Load(xmlUrl);
XmlElement root = doc.DocumentElement;

try
{
    totaal = Int32.Parse(doc.GetElementsByTagName("Totaal")[0].InnerText.ToString());

    // Create lists with values
    XmlNodeList namen = doc.GetElementsByTagName("naam");
    XmlNodeList ptypen = doc.GetElementsByTagName("ptype");
    XmlNodeList ids = doc.GetElementsByTagName("id");

    // Door het totaal heen itereren
    for (int i = 0; i < totaal; i++)
    {
        // Create variables of it
        int id = Int32.Parse(ids[i].InnerText.ToString());
        int ptype = Int32.Parse(ptypen[i].InnerText.ToString());
        string naam = namen[i].InnerText.ToString();

        // Check if ID exists
        int tot = this.tbl_klantTableAdapter.GetData(id).Count;
        if (tot == 0)
        {
            // New item, add
            this.tbl_klantTableAdapter.Insert(naam, ptype, id);
        }
        else
        {
            // Existing, update
            this.tbl_klantTableAdapter.Update(naam, ptype, id);
        }
    }
}
catch
{
    // Rest of code
Was it helpful?

Solution

Dispose Your nodelists after the loop may help

System.Xml.XmlNodeList tempNodelist = Your stuff;
                IDisposable disposeMe = tempNodelist as IDisposable;
                if (disposeMe != null)
                {
                   disposeMe.Dispose();
                }

OTHER TIPS

XmlNodeList implements IDisposable, so you can call namen.Dispose() (also for the other XmlNodeList objects) to force the objects to be discarded and cleaned up.

Yes, you definitely should use the XML stuff locally and dispose after using the XML stuff. The xml stuff seems to ocupie large memory blocks.

You should use nameX.Dispose() and nameX=null to free up the memory used for these temporary xml objects.

You may use GC.Collect() to force memory collection: http://blogs.msdn.com/b/stevenpr/archive/2004/07/26/197254.aspx.

You may also use remote .Net performance viewer to get insides on memory usage: http://blogs.msdn.com/b/stevenpr/archive/2006/04/17/577636.aspx

If your app is consuming much memory before calling into the sync task, you may consider of creating a new application with a separate process for the sync stuff. You can also free up memory for your process when you move functions to a library. WM6.1 and higher have a new memory slot for compact Framework libraries, so the main process memory slot is not lowered: http://blogs.msdn.com/b/robtiffany/archive/2009/04/09/memmaker-for-the-net-compact-framework.aspx

If you need more help you should provide more details/code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top