webBrowser slows down eventually stops working (memory leak?)

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mark106
Posts: 27
Last visit: Thu Mar 21, 2024 2:29 pm
Has voted: 1 time

webBrowser slows down eventually stops working (memory leak?)

Post by mark106 »

Product, version and build:
5.4.138
64 bit version of product:
Operating system:
Windows 7 Pro SP 1
64 bit OS:
PowerShell Version:
V5

Hi,

I've have created a PowerShell Studio GUI which uses the WebBrowser control and this has been working fine. I recently got the webBrower to invoke a JavaScript function (to update some content) .........

$webbrowser1.Document.InvokeScript("reload")

...........and this seems to work ok. However, if this Javascript function is repeatedly triggered over time the WebBrowser starts to stutter and become unresponsive. This eventually causes the GUI to become unresponsive as well. I have also noticed that invoking the JavaScript function causes the exe's memory usage to increase slightly each time.

The same process has been replicated in IE11 and Chrome 37.0.2062.124 and here the problem doesn't occur (no slow-down or memory increase).

I've seen mention of a memory leak in the .Net WebBrowser control and although I can see the memory usage increase when invoking functions from the webBrowser control, it's not huge but I suspect it's related to the performance problem.

I made a new, blank project, and added in a webBrowser and the command to invoke the JavaScript function and it has the same problem.

Any ideas of how I might be able to get around this?

Many thanks,

Mark
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: webBrowser slows down eventually stops working (memory leak?)

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: webBrowser slows down eventually stops working (memory leak?)

Post by jvierra »

Without the code and the web page there is no way to know why this is happening.
User avatar
mark106
Posts: 27
Last visit: Thu Mar 21, 2024 2:29 pm
Has voted: 1 time

Re: webBrowser slows down eventually stops working (memory leak?)

Post by mark106 »

Hi,

The code is essentially just:

$webbrowser1.Document.InvokeScript("reload")

......as I made a new GUI with just a WebBrowser control and a button to execute the above line.

I don't think the problem is limited to a particular webpage. I noticed that I was getting a lot of JavaScript errors in the webBrowser control and found that setting the browser emulation (https://msdn.microsoft.com/en-us/librar ... _emulation) got rid of most of them. Unfortunately, the memory/slow-down issue remained.

I spoke to the author of the webpage that I'm using in the embedded browser and he reckons it's a garbage collection issue. I Found someone with a similar issue on Stack Overflow.........
http://stackoverflow.com/questions/8302 ... er-control
Having battled this exact Out of Memory issue from different directions (Win32 WorkingSet / COM SHDocVw interfaces, etc.) with the WPF WebBrowser component, I discovered the problem for us was jqGrid plugin holding onto unmanaged resources in the IE ActiveXHost and not releasing them after calling WebBrowser.Dispose(). This issue is often created by Javascript that is not behaving properly. The odd thing is that the Javascript works fine in regular IE - just not from within the WebBrowser control. I surmise that the garbage collection is different between the two integration points as IE can never really be closed.

One thing I'd suggest if you are authoring the source pages is to remove all JS components and slowly adding them back in. Once you identify the offending JS plugin (like we did) - it should be easy to remedy the problem. In our case we just used $("#jqgrid").jqGrid('GridDestroy') to properly remove the events and associated DOM elements it created. This took care of the issue for us by invoking this when the browser is closed via WebBrowser.InvokeScript.

If you don't have the ability to modify the source pages you navigate to - you would have to inject some JS into the page to clean up the DOM events and elements that are leaking memory. It would be nice if Microsoft found a resolution to this, but for now we are left probing for JS plugins that need cleansed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: webBrowser slows down eventually stops working (memory leak?)

Post by jvierra »

The web page and JavaScript are all part of the code.

The Web control is essentially IE but it defaults to IE 8 compatibly and it is essentially unconfirmed. You have not proven a memory leak. ON a modern machine a memory leak would not slow down the responsiveness.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked