UWP memory leak using webview in MVVMCross application - memory

I have a MVVMCross UWP application.
The first views are not remarkable until the user comes to a welcome page and from there goes to a dashboard.
Here lies a problem, somewhere is a memory leak and I can't find it.
The graphic shows memory usage in MB and navigations from welcome->dashboard and back.
The first section is navigating to a dashboard without an embedded webview, the second section has a webview element which loads a rather complex page with embedded video.
Some notes:
The webview, when enabled is loaded in code behind using the SeperateThread flag and set as the content of a ContentControl
On unloading the view I explicitly set the content of the ContentControl back to null, the temp value to null that holds the webview and manually call GC.Collect. If I do not do this the webview will continue playing the video in the background until the GC kicks in
I use a custom presenter (adding support for popups and to implement and call IDispoable/Dispose for my viewmodels (to not wait for the GC to clear subscriptions and such)
When analyzing memory dumps I can not see any changes in managed objects or memory
There seems to be a problem (a small leak) when not using the webview and I will look into this as well, right now I am more concerned about the second scenario
Where should I continue to ivestigate what is going on?
Thank you!
memory over several hours
EDIT:
Test Application available (see comments)
Just let it run for about 30 minutes to see the problem. Memory will steadily increase slightly until the application crashes (might take a few hours).
I kinda messed up the display and % for the memory usage. just watch application memory in task manager or export a csv file with the raw data
Same progression in sample app

Related

iOS - Terminated due to memory issue

I'm having a memory issue with one specific ViewController and memory. When I launch my app in debug mode, it launches with 40mb memory usage. (I have no idea if this is already a lot or not -- what is common?). Then, when I open this specific view, it spikes up to about 120mb. The issue is, when I pop this view (with the navigationController popViewController), the memory stays up at 120mb. And then, when I re-open it, it spikes to 200mb (a 80mb increase every time).
With other similar ViewController it spikes up to 120mb too, but when I close the view, it goes back down to more or less 40mb.
The problem is that this specific view contains quite a bit of code (about 1000 lines...) and it's impossible for me to post everything here.
What methods should I use to specifically locate the issue in xCode?
For anyone I might be able to help with this:
use the tools in xCode as recommended. There are some great tutorials online.
In my case it was an issue with an [NSTimer] which kept a strong reference to my view, so it never got released afterwards, thus stacking up memory. Make sure to stop your time when you pop a view.

memory leak between page reloads on safari in ios

I have a three.js website which when loaded in mobile safari on iphone takes up ~450mb(I found this through "Instruments" utility in XCode).
Now, when I reload this page, the inspector shows that the memory is now ~550mb. And, the next time I reload the page, it crashes altogether(probably due to crossing the memory limit of 640mb?)
I've tried:
Disabling renderer.render calls(three.js sends data from main memory to GPU memory during renderer.render) so nothing is sent to GPU, in this case the memory was ~280mb but still, it was increasing with page reloads.
Disabling caching on the main page.
Freeing up almost all the resources both in main memory and in GPU memory as suggested here.
Set any/all images src to 1px data URL as suggested here.
But, the page still crashes due to increased memory between reloads. So, I am confused as to what keeps the browser from freeing up the memory. Any clues?

increasing memory allocation on iOS application

I've developed an iOS application. There is a main screen which is displaying a google map and there are many markers, polygons etc in it.
At the beginning app was using around 120MB of memory.
- I touched settings button of my app and went to the settings page. there is no code. only segue connection in the storyboard (red line on the image)
- then I came back to the map screen (white line on the image)
You can see the memory allocation. every time when I open the map screen memory usage is increasing
What is the problem. What should I do?
The graph you have captured in Xcode is a decent overview of your memory consumption, but I'm afraid you are going to have to use more specific tools to diagnose this leak: Instruments comes shipped with Xcode and will help you track memory leaks; you will have to research (at least) the Leaks and Allocations instruments to figure out why your View Controller isn't being released.
While it is impossible to diagnose without seeing your code, that graph heavily suggests that your map view controller is not being released (hence the steady growth each time you create a new one)
To hazard a guess, I'd imagine you are creating a new mapView in viewDidAppear rather than viewDidLoad

UIWebView memory leak

I have an iPad app (IOS 4.3 & 5.0) that is creating a UIView with at least 2 subviews every time the user pages forward or backward. The 2 subviews are UIWebView objects.
On every relevant swipe the old UIWebViews are removed and deallocated properly (removeFromSuperview then stopLoading and set delegate to nil) and the parent UIView is deallocated. I can confirm that both parent UIView and the 2 child UIWebViews are absolutely gone.
I can see the retain count on the webviews as they are being removed and I am quite sure that they are indeed removed every time.
However my memory allocation continues to increase on every swipe by ~200-350k . No leaks in instrument but I can see the memory usage going up slowly.
On iPad 1 the app eventually (within 6-10 minutes of moderate usage) receives multiple memory warnings and is terminated. LowMemory crash log...etc is generated.
We use stringByEvaluatingJavaScriptFromString and loadRequest to populate the Webviews. Memory usage creeps up even when nothing is injected or loaded into the webViews.
Is anybody experiencing this kind of behavior with UIWebViews ? Has anyone dealt with this successfully ?
Thoughs, comments & answers would be greatly appreciated.
Try to use Instruments but with the "Allocations" template and watch the "# Living" columns for UIWebView. I usually use it like this:
Profile app with Allocations template
"Warn up" the app by going thru all tabs, scroll around etc.
Press "Mark Heap", this will create a "Baseline" heapshot
Do the thing you think causes objects to stay around
Press "Mark Heap" again, this will create a "Heapshop #" heapshot
Inspect the objects in "Heapshop #" which will show size and number of objects created and alive since the last heapshot.
Goto 4
Not aware of any leak, but if you are destroying and re-creating the same view hierarchy every swipe, why not store the views and re-use them instead?
Regardless of whether they leak, webviews are computationally expensive to set up, so re-using them and just reloading the content should give you a performance boost, and may solve your leak as well.
are you still seeing this issue? I traced something which looks like unbounded memory usage (not necessarily a leak, but I get low memory warnings and eventual process termination) by calling stringByEvaluatingJavaScriptFromString over and over:
[webview stringByEvaluatingJavaScriptFromString:#"something()"];
It seems like there is something odd there.

What is the difference between applicationDidReceiveMemoryWarning , didReceiveMemoryWarning?

What is the difference between applicationDidReceiveMemoryWarning and didReceiveMemoryWarning? What is the best way to handle those warnings?
You should be using "Lazy loading" of data on all your views.
For example, if you are displaying a list of data on a table view, you should release the data when your view controller receives a didReceiveMemoryWarning and set the data pointer to nil.
Most of the Apple sample code emphasizes on this lazy loading technique.
applicationDidReceiveMemoryWarning is a similar message sent to your AppDelegate. You should release unwanted global data that you store in your AppDelegate.
A classic example is Safari. Open say 2-3 tabs in Safari (on iPhone Simulator). Let all the tabs load the web page content. Try switching between different tabs. You should notice that the content remains there and is not flushed. Now from the menu, close "Simulate Memory Warning". Now when you switch to a different tab, Safari will fetch the contents again. Internally what has happened is that, the URL is remembered, but the entire web page contents was released in the didReceiveMemoryWarning method.
You should also implement similar mechanisms in your app.

Resources