Can anyone please tell me how to handle memory management in iOS Cordova based project with "Received memory warning"
I am getting this warning in iOS Cordova project when running on my iPhone or iPad (version 8.1).I am using CDVlocation for geolocation in my app. I am mainly receiving this message while loading map based views.I am using an ARC based Xcode project
Any help in managing memory warnings with cordova "Received memory warning" will be appreciated.
Thank you all
Try this way in CDVPlugin.m
- (void)onMemoryWarning
{
// override to remove caches, etc
NSLog(@"onMemoryWarning");
NSString * javascriptString = @"yourJSFunctionToManageMemoryWarnings();";
[self.webView stringByEvaluatingJavaScriptFromString:javascriptString];
}
When your iOS app receives a Received memory warning
notification, iOS is trying to reclaim as much memory as it can because some app needs it. That app could be you, or it could be someone else. When this happens, all currently executing apps (not just yours) get this notification, and each app should respond by freeing as much unused memory as it can. Since Javascript does its own memory management, all a Cordova app can really do is dereference memory by setting variables to null (or some small value), so myHugeGlobalObject = null;
, myArrayOfOneMillionThings = [];
, etc., so that the garbage collector can then free that memory the next time it runs. So, when you receive a Received memory warning
notification, that is what you should do.
In CDVPlugin.m, you would do something like this to deliver the iOS notification to your Javascript app:
- (void)onMemoryWarning
{
// override to remove caches, etc
NSLog(@"CVPlugin.m: Got onMemoryWarning notification");
NSString * javascriptString = @"yourJSFunctionToManageMemoryWarnings();";
[self.webView stringByEvaluatingJavaScriptFromString:javascriptString];
}
and then in your Javascript (in index.html
, let's say):
<script>
function yourJSFunctionToManageMemoryWarnings() {
console.log("yourJSFunctionToManageMemoryWarnings() was called");
myHugeGlobalObject = null;
myArrayOfOneMillionThings = [];
}
</script>
It sounds like in your particular case iOS is attempting to make memory available because it is YOUR app that needs it (in order to load the map view) and what you would do is just what you would normally do: free unused memory as described above, and when everyone else also politely does the same, you'll get your memory.
That said, if you are experiencing some type of out-of-memory condition beyond what you described here when trying to load the map views, you may want to look into some advanced Javascript memory management techniques like object pooling (http://www.html5rocks.com/en/tutorials/speed/static-mem-pools/) where you basically grab all the memory you expect you'll need at startup and then you know you'll have it when you need it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With