I've been creating a Tabbed Browser application and am wondering about the garbage collection. I've zeroed out all of the variables, but I am wondering about the instances of the browser I am using when creating and deleting tabs.

I am using the following code to call up an instance of the WebBrowser (exBrowser) and placing it in a new Tab...

Dim tab As New TabPage
        Dim brws As New exBrowser
        wb = brws
        brws.Dock = DockStyle.Fill
        tab.Text = "tab"
        tab.Controls.Add(brws)
        brws.ScriptErrorsSuppressed = True
        Me.TabControl1.TabPages.Add(tab)
        Me.TabControl1.SelectedTab = tab
        brws.Navigate2(strHomePage)

When I choose to Close or Delete a Tab, I am using:

TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)

What happens to the WebBrowser instance (wb or brws) when I am deleting a Tab in this manner? Should I also be emptying this out as well in some way? I want to keep memory usage down and also believe this is still running despite deleting a Tab.

How should this object instance be handled insofar as it's creation and removal from memory? Thanks!

-Tom

Recommended Answers

All 12 Replies

I am not for certain, but unless you dispose of a control, it could still be taking up space. Try adding this piece of code right before you remove the tab.

TabControl1.SelectedTab.Controls(0).Dispose()

Hi Codeorder,

Well last night it was verified, as when I closed a tab the media content on that page kept playing (I could hear it going). I will add this piece of code today and report back here how it worked out. THANKS!

-Tom

:icon_wink:

I tried this:

TabControl1.SelectedTab.Controls(0).Dispose()
            TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            If wtab1 > 1 Then
                TabControl1.SelectedIndex = wtab1 - 1
            End If

But the memory didn't decrease. I did watch it increase. Not from adding a tab but when a webpage was loaded into it, it went up about 3k.

For my knowledge, did .Dispose() stop the media content of that certain page after closing the tab?

About the memory usage, the possibility could be that the content from loading a web page is stored in the Temporary Internet Files folder and while your application is still active, the files remain stored in the memory.
The following code should load up the Temporary Internet Files folder.
Refresh Windows Explorer if needed after loading a page in your web browser.

Dim myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
        Process.Start(myFolder)

:-/ Would clearing the cache for the time you viewed pages in a tab help after closing the tab?
Would doing so, and viewing the same page, have your Processor work extra?

Here are some stats on the Memory Usage...
(remember this is a Dual-Browser project):

24,156k - Before Loading of Any Webpages
32.120k - Both WebPages Loaded In
30,380k - After Both WebPages were Loaded In
30,464k - 1 Tab Added
34,476k - WebPage opened in New Tab added
34,436k - After Loading Webpage
46,756k - Surfed to YouTube
46,564k - After YouTube Loaded
63,164k - After Closing Tab (?)

It DID stop the content from continuing playing when I closed the tab with the Dispose() command, though!

I have Internet Options in the MenuStrip so I accessed that and deleted the temp files there. (It had been a while, there were many.) Then I closed the app (Running in a Debug mode) and re-ran again...


32,560k - After initial 2 WebPages were loaded
32,604k - Added New Tab
36,500k - Surfed to a WebPage in new tab
34,604k - After Deleting the new Tab
33,168k - Re-Added a New Tab
35,312k - Surfed to Same WebPage again
35,316k - Added Another New Tab
37,004k - Surfed to same WebPage as 1st added Tab
36,984k - Deleted 2nd Added Tab/Page
35,532k - Deleted 1st Added Tab
35,580k - After Deleting IE Temp Files manually


Additionally, the program now 'hangs' and I need to end the task. Am unsure why as of yet on that, it could be due to the PC needing a reboot.

Also, the Delete Temp Files does not delete everything contained in the temp files folder.

I'd had an instance of the installed version running, so I will retry later without it, in case there is some kind of conflicting/shared problems. (ATM I need to go to town.)

Thank you for the provided information, for it can be useful to refer to when working with web browsers.

Agreed on that! :)

I did think it was quite odd that when I had deleted the Tab in the first set of results the memory usage jumped UPward, though. I had fully expected it to go down a little at least.

A shoutout that I am still here...

I think I am going to bring the source in for an instructor to take a look at and see what she says about it. This isn't part of any kind of assignment, I'm being ambitious as I've only had intro VB.Net thus far LOL but this way I can copy it into a station there and compile to retest it to see what I come up with.

I know I wont release it until it's taken care of. Releasing memory is such an important aspect of good programming, and I'd like it to run as fast and freely as possible.

The problem is nesting this object within an instance of a tab, I think. Destorying the tab isn't enough, and your suggestion did stop things like a media stream in the background when a tab is closed helped, but somewhere, somehow, there is still memory being used up.

I'll report back and let you know what I find out.

Something to look forward to. :)
p.s. Nice information provided about:

Releasing memory is such an important aspect of good programming

I am glad to know that others look into such aspects, especially when they are capable of doing so.

Thank you for the provided information, for it can be useful to refer to when working with web browsers.

Okay, I believe I am onto something here. When Tab instances are being deleted, the Web browser object instances are not being deleted.

My current problem, then, is how to Identify what WB Instance is paired with what Tab instance.

' Add a Tab to TabControl1
        Dim tab As New TabPage
        Dim brws As New exBrowser
        wb = brws
        brws.Dock = DockStyle.Fill
        tab.Text = "blank"
        tab.Controls.Add(brws)

That is what I am using to Create a Tab. Here is what I am using to Delete a Tab

TabControl1.SelectedTab.Controls(0).Dispose()
            TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            If wtab1 > 1 Then
                TabControl1.SelectedIndex = wtab1 - 1
            End If

I truly believe that the solution is in removing the WB instance related to the currently selected Tab before removing the Tab from memory. I'm just not sure right off as of yet how to accomplish the feat, or how to ID the right WB Instance related to the currently selected Tab Control in order to dispose of it properly.

I figured that using:

TabControl1.SelectedTab.Controls(0).Dispose()

and the selected tab's WebBrowser being control(0), it would Dispose of it, as in deleted permanetly from the application.

" object instances " sound foreign to me, although I am a novice programmer.
Googling as speaking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.