Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

When using a re-usable form (the user can open as many as they wish of the same screen) I normally do a
VB
Dim NewFrm as New Form1

for example

If that form contains some List(of declarations, a richtextbox and several controls, when the form is closed, for example
Dim MyList as New List(of String)
    Richtextbox1.text="populated with some data"


then on the formclosing sub, I would clear the List by doing something lke
MyList.Clear
Mylist.Trimexcess
MyList=Nothing
Richtextbox1=Nothing
Me.Dispose

But if the form has been opened several times and are all closed, the memory is not released back into the system, it just keeps accumulating.

How can I ensure that, if I am opening a form using the method of Dim AS NEW FORM that when it is closed, I am reclaiming the memory back to the system?

Thanks!

What I have tried:

Disposing and setting objects to NOTHING wherever possible
Posted
Updated 4-Feb-21 1:05am

You don't. The memory is released back to the system, but it isn't made available to the system for recycling until it is garbage collected - and that only normally happens when your app requests a chunk of memory that can't be allocated - then the Garbage COllector is kicked into action and starts freeing up unused memory (and moving stuff about if it has to).

This gets quite complicated: Fundamentals of garbage collection | Microsoft Docs[^] but the basic rule is: don't worry about it, the system is better at this stuff than you are! :laugh:
If you really need to release resources though, you should be implementing a Visual Basic Destructor - Tutlane[^] rather than trying to manually do this in the FormClosing event - if nothing else, not all forms are unused once they are closed: think about a FileOpenDialog where you access the file name and path in your code only after the form is closed!
 
Share this answer
 
Comments
Member 12561559 4-Feb-21 7:55am    
Hmm. Thats what I thought as I have read that, but I am getting "could not get a handle on the window" type errors and eventual shut-down of the app itself - it is connected to MySQL though, perhaps it is how I am utilising it - I have one connection that is made during the start of the application, perhaps I should be opening and closing only when I am actually doing anything with it - I'll do some searching on that side. Memory is disappearing somewhere to the point it has a moan about handling the window and then closing the app completely. Thanks Griff
OriginalGriff 4-Feb-21 8:21am    
Yes, you should - create, open, use, dispose: a Using block is the way to go as it does all that for you.

Bear in mind that actual memory isn't the only thing that can cause problems: handles are very scarce resources, so unless you are disposing of Graphics contexts, Fonts, Pens, windows, and a whole pile of other stuff you can get very similar problems without getting anywhere near memory limits (which are HUGE because they include you HDD free space as virtual memory as well!)
How are you measuring the memory allocation?

If you're just looking at the "memory" column in Task Manager, that's not going to give you an accurate result. It shows you the "working set" of your application, which won't decrease unless the OS is under memory pressure.

You need to use a real memory profiler to profile your application if you want to find memory leaks. It looks like there's one built in to VS2019[^], but I'm not sure whether it's supported across all editions.
 
Share this answer
 
Comments
Member 12561559 4-Feb-21 7:56am    
Correct, not just monitoring TM, but getting a window handle error as well, before the app completely shuts down - as per my reply to Griff above, I am also connected to MySQL with one connection made when the app starts and that is left open throughout the entire session of app usage. I have checked all my adapters to make sure I am disposing them correctly so perhaps it is something on that side of things. Thanks Richard - most appreciated that you replied
Dave Kreskowiak 4-Feb-21 10:05am    
You can get "Out of Memory" errors if Windows is running out of handles.

Somewhere in your code, you're using resource but not releasing them. Go through your code and make sure you're calling Dispose on every object that exposes a Dispose method when you're done using that object.

For example, if you're new'ing up xxxSqlConnection objects, using them, but never calling Dispose on them when you're done, you're consuming handles and never releasing them.

The same goes for most GDI objects, like Bitmap, Brush, Pen, Font, ...
Member 12561559 5-Feb-21 3:54am    
Will do Dave ! Thanks fella

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