Open jgold6 opened 4 years ago
Additional note: This is not exclusive to a Document based app. I could see the exact same behavior in a non-document based app if I just open and close new windows that have a WKWebView.
Curious. I can reproduce. Great sample @jgold6 as usual!
Something strange is going on. Things I've tried so far unsuccessfully include:
So the problem is that Xamarin.Mac isn't disposing the documents (and thus their view controller) promptly when closed.
You can see this by adding
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
System.Console.WriteLine("Dispose Doc");
}
to the Document and a similar print to the View Controller.
If you add something like this:
new System.Threading.Thread (() =>
{
while (true) {
System.Threading.Thread.Sleep (100);
GC.Collect ();
}
}).Start ();
In the AppDelegate, you'll see the dispose calls in 100-200 seconds after close, and the stray processes will go away soon afterwards.
Obviously that isn't an acceptable solution. I'm looking into if we have a hook on document close to dispose promptly.
Ok, it isn't the VC or the webview:
This no good bad ugly hack does not make them go away either.
public override void ViewDidDisappear()
{
base.ViewDidDisappear();
NSTimer.CreateScheduledTimer(2.0, t =>
{
BeginInvokeOnMainThread(() =>
{
this.webView.Dispose();
this.Dispose();
});
});
}
Cool, but do see my note above that this issue is not exclusive to Document based apps. I could see the same behavior if I make a non-Document based app and just push a new window with a WKWebView (tested pushing a modal window).
As a side note, and I should have mentioned it, I had already tried Disposing of the WKWebView and the ViewController and nulling them out, but that did not alter the situation.
Any updates on this?
No, no updates yet, because it's a rather tricky problem to solve without affecting other things (just blindly calling Dispose is the wrong thing to do, because it might have undesired side-effects).
My recommendation would be to do a GC.Collect manually a little bit after a WKWebView is closed:
public override void ViewDidDisappear()
{
base.ViewDidDisappear();
NSTimer.CreateScheduledTimer (1.0, t => {
GC.Collect ();
};
}
Not a great solution, but the best one available at the moment.
Description
Reported in Xamarin.Forums: https://forums.xamarin.com/discussion/comment/399638#Comment_399638
With a simple document based Xamarin.Mac app that only has a WKWebView, every time a new document is opened, two new processes are spawned 'APP_NAME Networking' and '' (the latter is a 'APP_NAME Web Content' process renamed in Activity Monitor), e.g.:
When the document window is closed, the two processes remain. If you continue to open and close more windows, then more and more of the above two processes remain, i.e. open and close 10 windows, then there are 10 each of the above processes that remain.
Doing exactly the same thing in XCode in Obj-C, after closing a document window, the above two processes go away almost immediately, so the above processes only exist while the window that spawned them is open. i.e. open and close 10 windows and none of the above processes remain.
Back in Xamarin.Mac, I noticed that if I call GC.Collect() when a window is closing, then all but one pair of the above processes will go away, i.e. open and close 10 windows and there will still be one pair of the above processes remaining. The same is true even if I call GC.Collect() multiple times.
Doing another test by forcing memory pressure by creating a bunch of 1MB byte[], I observe that all of the spawned processes will quit and go away.
Below I have provided both a Xamarin.Mac test app and an XCode test project.
Steps to Reproduce
Expected Behavior
When a document window is closed, the processes spawned by the WKWebView will quit. This is what happens with the XCode test project.
Actual Behavior
When a document window is closed, the processes spawned by the WKWebView do not quit, they still appear in Activity Monitor.
Environment
Build Logs
N/A
Example Project (If Possible)
TestOrphanProcesses-XamMac.zip
TestWKWebView-Obj-C.zip