Garbage Collector on Compact Framework

Hi all,

I have been having some issues with the Garbage Collector with the Compact Framework in that I don't feel it runs when it needs to run, so I therefore have to put in my own GC.Collect() to free up the memory on my device so that the application can run efficiently.

Does anyone know of any articles that are based on the Garbage Collector on the Compact Framework I have read articles based on the GC for the .NET Framework, and these recommend against calling GC.Collect() for efficiency reasons, but I have been forced to call this on my application due to the Garbage Collector just not running that often.

Does anyone have any more info on this

Thanks



Answer this question

Garbage Collector on Compact Framework

  • Angry Coder

    Just touching back on this, Iyla, I remember we had to remove all the Dispose() calls on the MainMenus that we had on the Forms' because we were getting some Exceptions with regards to the SIP Control, in that if we Disposed on a MainMenu Control on one Form that had the SIP, and then tried to access the SIP on another Form's MainMenu, then an exception was raised.

    Tryst


  • mr4100

    OK - Having just read Steven Pratschner's Blog on the GC for the Compact Framework, it seems that static variables get marked on a GC, and thus do not get cleaned up from memory - so my assumption from this is that static variables stay alive for the whole of the life of the application.

    This extensive object structure that I mentioned in my last post is actually sitting in a static variable, so I my object structure is going to be alive from the time it is created, 'til the time the application closes.

    Is there anyway I get the GC to clean static varaibles


  • AlpanaDhole

    I, too, have had issues -- mainly OS messages which tell the user memory is low and they need to choose an application to close.

    To prevent this from happening, I place garbage collection statements at strategic points in my code – which commonly include use of some collection like an ArrayList or HashTable.

    You might want to include a call to WaitForPendingFinalizers so the application will free up unneeded memory prior to starting something new.

    GC.Collect;

    GC.WaitForPendingFinalizers();

    --Finkster


  • Tryst

    Here is a blog writing about .NET CF GC:
    http://blogs.msdn.com/stevenpr/archive/2004/07/26/197254.aspx

    And a whole series about .NET CF Performance:
    http://blogs.msdn.com/davidklinems/archive/2005/12/01/499017.aspx



  • yaron-ct

    If you had to do that it probably means you have issues with your code, e.g. you're not disposing of IDisposable objects. These objects allocate unmanaged recourses which are out of GC control and they would not trigger GC. You should review your code and make sure these objects are disposed of properly.



  • Ajaykr

    No, I don't get the out of memory exception. What I do get though is an exception when attempting to load the Symbol Imager/scanner API's when the device gets to a certain memory size. I recall Symbol telling me that their Imager/Scanner API's, that make use of the Compact Framework, make use of around 8 MB, so if the device had less than this left and I tried to enable the Imager/Scanner then an Exception would be thrown.

    Now I seem to be getting an Exception from the Imager/Scanner with more than 16 MB left.


  • hellopower

    If you’re not getting OOM exception, there's no "leak" and your scanner issue has some other reason. There's, however, a known issue with GC not firing if native memory allocation failed as there's no notification from OS to trigger GC. That’s one of a few legitimate reasons to force GC before doing native side operations which known to use a lot of memory.



  • Lamar Miller

    But I am not making use of unmanaged code in the scenario where my application eats up memory without freeing it.

    I simply have my own custom classes (Question, Answer etc) that contain either int, string, or collection classes as their properties. For example, one of the classes is as follows. What I do think is taking up all the memory is that I have an object structure that can be quite deep. For example:

    Form:
    Group
    Question
    Question
    Question
    Question
    Question
    Group
    Question
    Question
    Group
    Question
    Question
    Group
    etc etc (In some instances I have groups within groups and upto around 20 Questons can fall under 1 group - this is what I think is eating my memory).

    public class RecNspInspectionQuestion : MCS.PocketPC.Record, ICloneable
    {
    public enum Columns { QuestionTreeUID, ParentUID, QuestionTypeUID, QuestionNodeTypeUID, ShortDescription, LongDescription, IsActive, IsRequired, Reference, SortOrder, Notes, IsSystem, AnswerSetTypeUID, End };

    public int m_nQuestionTreeUID = -1;
    public int m_nQuestionTypeUID = -1;
    public QuestionNodeType m_QuestionNodeType = QuestionNodeType.Undefined;
    public int m_nParentUID = -1;
    public int m_nAnswerSetTypeUID = -1;
    public string m_strShortDescription = "";
    public string m_strLongDescription = "";
    public bool m_blIsActive = false;
    public bool m_blIsRequired = false; // Is it required that this question be answered.
    public string m_strReference = "";
    public int m_nSortOrder = -1;
    public string m_strNotes = "";
    public bool m_blIsAnswered = false;
    public bool m_blIsWrittenToDB = false;
    public bool m_blIsSystem = false;
    public int m_nAnswerStatus = 0;
    public InspectionQuestions m_colQuestions = new InspectionQuestions();
    public InspectionAnswers m_colAnswers = new InspectionAnswers();
    public TblNspInspectionQuestionType.RecNspInspectionQuestionType m_objQuestionType = new TblNspInspectionQuestionType.RecNspInspectionQuestionType();
    public TblNspAnswerSetType.RecNspAnswerSetType m_objAnswerSetType = null;

    #region Constructor
    public RecNspInspectionQuestion() { }
    #endregion Constructor

    public override string ToString()
    { return m_strShortDescription; }

    public object Clone()
    {
    RecNspInspectionQuestion rec = (RecNspInspectionQuestion)this.MemberwiseClone();
    InspectionQuestions colQuestions = new InspectionQuestions();
    InspectionAnswers colAnswers = new InspectionAnswers();
    //TblNspInspectionQuestionType.RecNspInspectionQuestionType objQuestionType = new TblNspInspectionQuestionType.RecNspInspectionQuestionType();
    TblNspAnswerSetType.RecNspAnswerSetType objAnswerSetType = new TblNspAnswerSetType.RecNspAnswerSetType();

    rec.m_colQuestions = colQuestions;
    rec.m_colAnswers = colAnswers;
    rec.m_objAnswerSetType = objAnswerSetType;

    rec.m_objQuestionType = (TblNspInspectionQuestionType.RecNspInspectionQuestionType)this.m_objQuestionType.Clone();

    // TODO: Clone objAnswerSetType.

    return rec;
    }

    /// <summary>
    /// Determines if the Question instance's Answer Object has Actions.
    /// </summary>
    /// <returns>True if Answer instance has Actions, else False.</returns>
    public bool HasActionsOnAnswer()
    {
    if ((m_colAnswers.Count > 0) && (m_colAnswers[0].m_colActionAnswers.Count > 0))
    return true;
    else
    return false;
    }
    }

    Is this Object structure overkill, do you think



  • AdriaanDavel

    So, you're not using any forms, bitmaps, menus, etc. at all Because if you do, you are using native code and native resources and you need to Dispose() of them.



  • Thelostcircuit

    If you want something to get freed by the GC you would need to get rid of all reference to it. To get rid of the reference held in a static variable you merely need to set that static variable to null.

    -Noah Falk
    .Net Compact Framework

  • Steven Syfuhs

    I think I remember some issues with images in image list which were not disposed with from. Go ahead and try disposing of them and see if that help.

     

    By the way, do you see OOM exception If you don't, there's no problem and GC runs OK.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



  • Khookie

    Yeah, I am using Forms, and am openning through the 'using' statement, which will automatically dispose of the Form's when they are closed (not in use).

    I am using images (GIF/JPEG), but these are used in ImageLists and then displayed in ListViews - I don't need to dispose of these images in the ImageList do I Also, I use MainMenu's, these also need to be disposed of do they


  • Garbage Collector on Compact Framework