1

I know there are few questions on this topic but I still haven't found a real answer to my issue.

I have an MDI form with a menu and of course clicking on a menu item opens up a form.

When this child form gets closed I can see that no memory is freed (according to Task Manager). I'm aware Task Manager is not very reliable, but it is reliable in the fact that if I open and close the form again and again the application slows down and the memory usage increases.

I found some topics here talking about disposing a form and being sure that no object from the form has some handles to other objects outside the form. In details I found this question and answer: Unregistering all events to release memory

the object that raises the event keeps alive the object that handles the event.

So this could be my case but I'm wondering if there is some way to find out whitch is the object that keeps alive the other. Is there any way to get a list of these objects? Can someone provide a few lines of code and/or be more clear about that? Thanks

EDIT: Some code

This is the main form containing the menu and working as MDI container.

Partial Public Class FrmMain
    Inherits Telerik.WinControls.UI.RadForm

    Public Sub New()
        InitializeComponent()
        .IsMdiContainer = True
        createMainMenu()
    End Sub

    Private Sub createMainMenu()
            radMenu1.Items.Clear()
            ' First MenuItem
            item = New RadMenuItem
            item.Text = reader.Value
            item.Name = "Registration"
            item.Tag = actualIndex
            item.Visibility = ItemVisibility
            AddHandler item.Click, AddressOf MenuItemClicked
            radMenu1.Items.Add(item)
    End Sub

    Private Sub MenuItemClicked(sender As Object, e As EventArgs)
        Select Case sender.Name
                Case "Registration"
                    Cursor = Cursors.WaitCursor
                    Dim frmRegistration As New FrmRegistration
                    frmRegistration.MdiParent = Me
                    frmRegistration.Show()
                    Cursor = Cursors.Default
        End Select
    End Sub
End Class

And this is the code of the form opened by main form menu

Public Class FrmRegistration
    Public MainForm As FrmMain
    Private myParent As Docking.HostWindow
    Public dbContext As New My_DataEntities

    Private Sub FrmRegistration_Load(sender As Object, e As EventArgs) Handles Me.Load
        loadQueries()   ' A lot of entity framework queries and loads

        ' some FrmRegistration's controls handlers

    End Sub


    Private Sub CommandBarButtonExit_Click(sender As Object, e As EventArgs) Handles CommandBarButtonExit.Click
        Me.Close()
    End Sub

    Private Sub FrmRegistration_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        If Not dbContext Is Nothing Then dbContext.Dispose()
        dbContext = Nothing
    End Sub

End Class

Loading entities uses around 80MB of memory and when I close this form it looks like this amount is not released. Each time I open this form the loading process adds around 80MB, appearing to be a memory leak after a few open/close cycles.

Community
  • 1
  • 1
Efrael
  • 557
  • 1
  • 4
  • 11
  • vb.net doesn't have in build memory management/release hence disposing an object manually should release the assigned proportion to the object, on the on_close event fired. – foo-baar Sep 20 '14 at 18:20
  • From MSDN: *["The .NET Framework's garbage collector manages the allocation and release of memory for your application"](http://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx)* However, it's possible to create memory leaks in vb.net. Could you post the shortest code to reproduce the behavior? – Bjørn-Roger Kringsjå Sep 20 '14 at 18:29
  • 1
    You are just wildly guessing at the problem, very low odds that you guessed right. Caused by you not using the tool you need to use to trouble-shoot this kind of problem. A memory profiler. – Hans Passant Sep 20 '14 at 18:43
  • Thank you guys! I will post some code. For Hans: have you any suggestion for a profiler? – Efrael Sep 20 '14 at 19:05
  • Code posted. This is the actual version but I made some attempts disposing objects, disposing FrmRegistration in the main form. Nothing worked. – Efrael Sep 20 '14 at 19:51

0 Answers0