Memory Leak after disposing a dialog?

2 posts, 1 answers
  1. Christian
    Christian avatar
    12 posts
    Member since:
    Sep 2014

    Posted 03 Oct 2014 Link to this post

    Hi

    ( Please if I'm wrong with my understanding of this rectify me )

    Reading about good and bad programming practices I heared that when calling a ShowDialog method (at least in a normal Windows.Form) that Form is not disposed after the DialogResult is sent, the Form remain in memory so it will be disposed manually, well, then following that good practices that is was I'm trying to do with a RadForm, this is the code:

    If ConfigureCorner.ShowDialog() = Windows.Forms.DialogResult.OK Then
     
        Telerik.WinControls.RadMessageBox.Show(Me,
                                               "Action applied",
                                               Me.Name,
                                               MessageBoxButtons.OK,
                                               Telerik.WinControls.RadMessageIcon.Info)
    End If
     
    ConfigureCorner.Dispose

    The problem is that every time that I call ShowDialog after the Form is disposed for first time the memory increases like 5-10 mb each time, and it needs much execution time to display the Form and its controls.

    I attached an image to show the Form contents, but his is the detailed content of the RadForm:

    · 3 different themes

    · 15 RadButtons
    · 5 RadCheckBoxes
    · 1 RadDropDownList
    · 8 RadGroupBoxes
    · 21 RadLabels
    · 1 RadPageView
    · 12 RadPageViewPages
    · 2 RadRadioButtons
    · 12 RadSeparators
    · 8 RadTextBoxControls

    · 2 Custom dialogs for open folder/files.

    As you see seems that I'm not using anything special that should 'cause this memory leak,
    the images of the controls are set directlly in the designer property grid as 'Image' property,
    I'm not loading or instancing any disposable object before closing the RadForm.

    Also, I'm trying to force garbage collecting aftear each call to Dispose method after calling ShowDialog, but that does not solve anything:

    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.WaitForFullGCApproach()
    GC.WaitForFullGCComplete()
    GC.Collect()


    This is normal?

    Any information will be very appreciated, thanks in advance.
  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 08 Oct 2014 Link to this post

    Hello Christian,

    Thank you for writing.

    The memory leak is caused by the fact that you are showing a new Form instance every time with the ShowDialog method. A quote from MSDN says: "Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application." You can refer to ShowDialog Dispose Memory Leak MSDN forum post which is quite useful about this topic.

    I would like to let you know that our controls provide rich UI capabilities and theming mechanism which inevitably increases the memory usage. Furthermore our components are built from the Telerik Presentation Framework which is built on top of the .NET Framework which once again, inevitably increases memory usage compared to other controls, such as the standard controls which simply wrap native objects. Unless a certain control has a memory leak it is eventually being disposed. Calling GC.Collect will not dispose the objects immediately, they will simply be marked for disposal and the next time the Garbage Collector passes it will decide whether they are suitable for actual disposal. This is due to the non-deterministic behavior of the Garbage Collector. Our objects are mostly from Generation 3, which means that they will be disposed at a later state (more about garbage collection). 

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top