[solved]how to release all the memory?
in main form, when a button is clicked, it opens a new form which has a webbrowser. users can use it to browser some sites. if the user closed this form, it goes back to the main form. and they can click button to open the browser form again.
I noticed that one thing. if the browser form is opened, it uses much memory (which is normal because of the site it browses). however, after the browser form is closed, the memory is not released.
In the browser form, when it is closing, the webbrowser is disposed and dereferenced. and no other part uses much memory in the form. GC is called too. what else should I do to release all memory the browser form uses?
now, it is like once the browser form is opened once, the memory usage never goes down even the form is closed.
any suggestions?
thanks
bear
Re: how to release all the memory?
Are you disposing of the form when it's closed too?
Re: how to release all the memory?
The memory is "release" and ready for re-use... by your application... it's still allocated to the app on the stack... so if you are to load another form, it can load back into the same location already allocated to your app. It's a speed thing... other wise it has to go and re-allocate new space each time. I'm not sure what there is you can do about it.
-tg
Re: how to release all the memory?
AFAIK, there is nothing you can or should do. How much memory is it using? I highly doubt it is more than, say, 50MB, and considering that no machine these days ships with less than 1GB, I doubt that anyone will notice it.
Re: how to release all the memory?
thanks for reply.
the problem is: the form is used as a test program, and the browser form will be called many times (and close). I noticed that, usually, the memory that it does not "return" is not much. however, when browsing certain site, i.e. too many images/flash/javascript, it will use much more memory, and this memory will not be "return". So, eventually, the program uses more and more memory, such as 200MB.
If the browser form is not launched, the main form is just a simple UI form. so, it should use much less memory. but, once the memory is-not returned, it seems never be returned (from taskmgr).
I minimized the form, dispose everything, set all new object as nothing, still the same result.
I understand the memory is still "used", and maybe it is for show the form quickly, but, I would think there is a way to "force" release the memory.
every time launch the browser form, I do this:
dim f as new browserform
f.showdialog()
maybe, I should not new it every time?
thanks
Re: how to release all the memory?
There are two ways to display a Windows Form: Show and ShowDialog. If you called Show, the form is automatically disposed when you call Close. If you called ShowDialog, the form is NOT automatically disposed. You can dismiss the form by calling Close or by setting the DialogResult property but, in either case, it is your responsibility to dispose the form. The most proper way to do that is with a Using block, e.g.
vb.net Code:
Using dialogue As SomeForm
'Initialise the dialogue here.
If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Get data from dialogue here.
End If
End Using
If you don't care how the dialogue was dismissed then you don't need the If statement. The important thing is that the object is created at the Using statement and then implicitly disposed at the End Using statement. You should do that for ALL short-lived objects, i.e. created and discarded in the same method, that implement the IDisposable interface. Note that that code is equivalent to this:
vb.net Code:
Dim dialogue As SomeForm
Try
'Initialise the dialogue here.
If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Get data from dialogue here.
End If
Finally
dialogue.Dispose()
End Try
Note that there is an implicit Try...Finally in a Using block. It won't catch an exception if one is thrown, but it will ensure that the object is disposed whether an exception is thrown or not and, if one is, whether it is caught or not.
Re: how to release all the memory?
I actually used it like this:
using fm as new browserform(val1, val2)
' some other init
...
fm.show
while (true)
' check one fm's property, if it is<0, then it means fm is closed.
' if it is closed, then quit while
' otherwise, sleep and wait
end while
end using
this part is in a control thread (STA)
but, it still have the same problem I described above. could it because I new/show the form from a thread? the memory usage varies a lot depends on the site the browser form shows (which is understandable). but it seems for each site it browses, it will keep certain memory, and never release it. when the site has too much rich content, it "reserve" much more memory too.
even after I close the browser form, dispose it, dereference it, the "reserved" memory is never released.
btw, I'm using 64bit win7 + VB 2010 express.
thanks
Re: how to release all the memory?
Why are you using Form.Show, and then using a silly loop to check it's dialog result? Why don't you simply use ShowDialog, which will only return when there is a dialog result to show?
Re: how to release all the memory?
it is because the form is show (or showdialog) in a thread. when the thread stopped, we need od next step for testing. so, I have to wait the browser form close. if use showdialog, it will work. but, I use show to see whether the memory used by browser form will be released.
unfortunately, no matter what I do, some part of the memory is never released. and as browser form is used for more time, this unreleased memory is adding up quickly, eventually, slows down the system.
thanks
Re: how to release all the memory?
one weird thing I noticed that is: if I use IE to browse to the same site (which use lots of memory), then close the tab, IE released most memory (which used for that site).
That's why I think it should have a way to release the memory used by browserform (it only has a webbrowser control and layout tab).
anyone knows how to do it? :)
thanks
Re: how to release all the memory?
If you dispose the form then any components created in the designer will also be disposed, so you don't need to do anything more to ensure that those resources are released. Anything you might be creating in code is another matter though. You are responsible for that cleanup yourself. If you do find that memory usage builds up then you can call GC.Collect and anything elligible for garbage collection will be cleaned up. That is generally not required but can be useful in certain circumstances. Its still up to you to perform the appropriate cleanup though, because anything that is eligible for garbage collection can't be collected.
Re: how to release all the memory?
Your problem is a memory leak in the webbrowser component,
I created a program which also uses a few webbrowsers.
On x64 machines memory usage got up to 200MB if you let it sit for say an hour or 2
With every navigation or page refresh memory stacked up.
Still didn't found the exact reason and fix.
But i am sure the problem is the webbrowser.
I removed the webbrowser from my program and let it sit for 24 hours, 1,5MB memory
My sollution (may or may not be perfect)
Create a class.
call it clsMemory
VB.NET Code:
Imports System.Runtime.InteropServices
Public Class clsMemory
<DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function GetCurrentProcess() As IntPtr
End Function
Public Sub New()
Dim pHandle As IntPtr = GetCurrentProcess()
SetProcessWorkingSetSize(pHandle, -1, -1)
End Sub
End Class
Then in your main form, after you close the webbrowser form execute it like this:
VB.NET Code:
'
Dim MemClass As New clsMemory()
MemClass = Nothing
Re: how to release all the memory?
wow! I just did a test, it works!
thanks, really appreciate it.
Re: how to release all the memory?
Quote:
Originally Posted by
DBP Roy
Your problem is a memory leak in the webbrowser component,
I created a program which also uses a few webbrowsers.
On x64 machines memory usage got up to 200MB if you let it sit for say an hour or 2
With every navigation or page refresh memory stacked up.
Still didn't found the exact reason and fix.
But i am sure the problem is the webbrowser.
I removed the webbrowser from my program and let it sit for 24 hours, 1,5MB memory
My sollution (may or may not be perfect)
Create a class.
call it clsMemory
VB.NET Code:
Imports System.Runtime.InteropServices
Public Class clsMemory
<DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function GetCurrentProcess() As IntPtr
End Function
Public Sub New()
Dim pHandle As IntPtr = GetCurrentProcess()
SetProcessWorkingSetSize(pHandle, -1, -1)
End Sub
End Class
Then in your main form, after you close the webbrowser form execute it like this:
VB.NET Code:
'
Dim MemClass As New clsMemory()
MemClass = Nothing
Special thank to Mr DBP ROY.
Your code helps me alot. :thumb: