Click here to Skip to main content
14,869,990 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi all,

I have a user control with several properties and events. This user control is filled with information of one entry in an SQL database.

The problem is listed at the bottom.
First, here is some information.

-- Not sure if relevant but have a suspicion --
some Variables in the control are declared as WithEvents. Those variables are of a class type that
I made in order to get around some circular reference issues. Essentially, it allows my control
to raise an event to the main application, informing it of what needs to be done. The application, having received the event and having references to all projects, can then perform whatever action is required by the control.

How this works..
Here is my class for allowing application to monitor the needs of all existing user controls.
Class ControlEvents

Public Shared Event ViewUser(Sender as MyControl)

Sub View(Sender as MyControl)
  RaiseEvent ViewUser(Sender)
End Sub

end Class

Here is the user control with a sub for viewing a user
Class MyControl
  Private WithEvents Subcription_ControlEvents As New ControlEvents

  Sub ViewUser()
    'Raise the event in the ControlEvents Class for the application to see
  End Sub
End Class

And finally, the applications Module watching for Control events and responding to them

Module UserControl_EventMonitor
   Private WithEvents Subcription_ControlEvents As New ControlEvents
   Private Sub MyControlClass_ViewUser(Sender as MyControl) Handles Subcription_ControlEvents.View
   'This is where I do Stuff to view the user.. Works Fine.
End Sub

End Module

Like I said, not sue if that's relevant to my problem.

This MyControl is used in a flow layout panel. Every time a search is run, the flow layout panel is looped through, disposing of all controls. the an sql query, then fill the FLPanel with new Controls.

!! and every time this happens the memory usage in Task Manager bumps up by 0.07 mb.
I have even tried calling the, warned against, GC.Collect() but no luck in freeing the ram.

If the search is repeated then the behavior continues until finally the out of memory exception is raised.

Anybody know whats going on here? I don't
Please help!

--------------- Update ------------------------------------------------------------
So it turns out that my Memory consumption comes from the #of sink blocks in use. From what I've read, these are weak references that are associated with a managed object. So why would calling garbage collector have no effect?
Updated 22-Dec-13 7:27am
Sergey Alexandrovich Kryukov 19-Dec-13 23:44pm
Who told you that disposing is related to memory deallocation? Why do you think you need to dispose anything in your particular control?
Mr.TMG 22-Dec-13 13:30pm
Because the object consumes resources and i assume that include memory. They were not being disposed of originally. It was only after I started getting the Out of memory exceptions that I started trying to dispose these objects.
Sergey Alexandrovich Kryukov 22-Dec-13 13:37pm
You do not understand. Managed memory is released be the Garbage Collector. And IDisposable.Dispose does not have to release any memory at all, even though it could be used, for example, to dispose umnanaged resources, through P/Invoke...
Mr.TMG 22-Dec-13 13:48pm
I think you are right and i don't understand. but there is defiantly an issue with memory consumption that is somehow related to clearing my user control objects from a flow layout panel and re-populating it. I'm not sure what the issue is and disposing was just an attempt to resolve the issue that clearly does not work. I'm definitely open to suggestion as I've never experienced this issue before. Thanks for your responses.
Mr.TMG 22-Dec-13 13:50pm
Memory consumption --Per perfmon-- appears to come from the #of sink blocks in use

Do NOT look in Task Manager to see how much memory your app is using. It's lying to you! Use PerfMon and the .NET Memory counters instead.

Task Manager is showing you how much memory the .NET CLR is RESERVED for your app, not how much it's actually using.
Mr.TMG 22-Dec-13 13:25pm
wow.. Perfmon is awesome! So it turns out that my Memory consumption comes from the #of sink blocks in use. From what I've read, these are weak references that are associated with a managed object. So why would calling garbage collector have no effect?
Sergey Alexandrovich Kryukov 22-Dec-13 13:46pm
This is not a fact that it has no effect. This is the first thing. The second one is: you cannot immediately see if GC has effect or not, it does not happen immediately. Everything depends on object reachability; and GC works on its own schedule...
Please see Solution 1, to get the idea.
Sergey Alexandrovich Kryukov 22-Dec-13 13:37pm
Agree, a 5.
You don't have a proven fact that your memory is leaking. See Solution 1. However, it does not mean that you cannot have memory leak, managed, unmanaged or both. You need to understand how memory clean up is done, in case of managed and unmanaged memory, as well as other resources. Please see my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Garbage collectotion takes care of all the memory management[^],
When CLR Do Automatic Memory Management and GC?[^].

Sorry for certain redundancy in those answer, but referencing them is still better than repeating it all over. Please read, hope it will help you to understand things.

Mr.TMG 22-Dec-13 13:53pm
Sergey Alexandrovich Kryukov 22-Dec-13 13:54pm
You are welcome. Will you accept the answer formally (green "Accept" button)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900