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.