Search code examples
c#windows-mobilewindows-mobile-6.1

C# Mobile - Memory warning (clear memory)


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

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();
                    }