Results 1 to 14 of 14

Thread: [solved]how to release all the memory?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    [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
    Last edited by FlyingBear; Oct 3rd, 2010 at 01:58 PM.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: how to release all the memory?

    Are you disposing of the form when it's closed too?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Using dialogue As SomeForm
    2.     'Initialise the dialogue here.
    3.  
    4.     If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.         'Get data from dialogue here.
    6.     End If
    7. 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:
    1. Dim dialogue As SomeForm
    2.  
    3. Try
    4.     'Initialise the dialogue here.
    5.  
    6.     If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
    7.         'Get data from dialogue here.
    8.     End If
    9. Finally
    10.     dialogue.Dispose()
    11. 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.
    Last edited by jmcilhinney; Oct 1st, 2010 at 11:10 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    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

  8. #8
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    New Member
    Join Date
    Oct 2010
    Location
    Steyl -> Limburg -> Holland -> Europe
    Posts
    11

    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:
    1. Imports System.Runtime.InteropServices
    2. Public Class clsMemory
    3.     <DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
    4.     Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
    5.     End Function
    6.  
    7.     <DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
    8.     Friend Shared Function GetCurrentProcess() As IntPtr
    9.     End Function
    10.  
    11.     Public Sub New()
    12.         Dim pHandle As IntPtr = GetCurrentProcess()
    13.         SetProcessWorkingSetSize(pHandle, -1, -1)
    14.     End Sub
    15. End Class

    Then in your main form, after you close the webbrowser form execute it like this:

    VB.NET Code:
    1. '
    2.     Dim MemClass As New clsMemory()
    3.     MemClass = Nothing

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: how to release all the memory?

    wow! I just did a test, it works!

    thanks, really appreciate it.

  14. #14
    New Member
    Join Date
    May 2011
    Posts
    1

    Re: how to release all the memory?

    Quote Originally Posted by DBP Roy View Post
    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:
    1. Imports System.Runtime.InteropServices
    2. Public Class clsMemory
    3.     <DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
    4.     Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
    5.     End Function
    6.  
    7.     <DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
    8.     Friend Shared Function GetCurrentProcess() As IntPtr
    9.     End Function
    10.  
    11.     Public Sub New()
    12.         Dim pHandle As IntPtr = GetCurrentProcess()
    13.         SetProcessWorkingSetSize(pHandle, -1, -1)
    14.     End Sub
    15. End Class

    Then in your main form, after you close the webbrowser form execute it like this:

    VB.NET Code:
    1. '
    2.     Dim MemClass As New clsMemory()
    3.     MemClass = Nothing
    Special thank to Mr DBP ROY.
    Your code helps me alot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width