You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I display two images on the page. When either is tapped, two new images are loaded. I've tried binding the src properties to an observable and updating this. I've also tried removing the Image elements and entirely replacing them. Both of these lead to high memory usage and crashes. It looks like the old images are never removed from memory.
Here is part of the code using binding. Memory usage jumps by ~30mb each time these images are loaded - even though the images themselves are only 87kb.
function loadQuestionImages(question) {
// ## Load both images before binding to ensure
// ## both display at same time.
var loadedImages = 0;
var imageSRC_1;
var imageSRC_2;
imageSourceModule.fromUrl('https://placehold.it/2000x2000')
.then(function (res) {
loadedImages = loadedImages + 1;
imageSRC_1 = res;
if (loadedImages == 2) {
imagesLoaded();
}
}, function (error) {
//console.log("Error loading image: " + error);
});
imageSourceModule.fromUrl('https://placehold.it/2000x2000')
.then(function (res) {
loadedImages = loadedImages + 1;
imageSRC_2 = res;
if (loadedImages == 2) {
imagesLoaded();
}
}, function (error) {
//console.log("Error loading image: " + error);
});
function imagesLoaded() {
pageData.set('imageSRC_1', imageSRC_1);
pageData.set('imageSRC_2', imageSRC_2);
animateInImages();
}
}
Hey @3rror404 thank you for reporting this one - indeed the memory consumption is rising and sometimes the app is crashing or freezing due to out of memory before the cache is cleared. We will look into this. Meanwhile, you can use nativescript-web-image-cache to load your online images.
With this plugin the memory consumption is normal but if needed you can force clean the cache as well.
e.g. (based on your test application) main-page.js
var imageCacheModule = require("nativescript-web-image-cache");
function loadQuestionImages() {
var imageSRC_1 = 'https://placehold.it/2000x2000';
var imageSRC_2 = 'https://placehold.it/2000x2000';
imagesLoaded();
function imagesLoaded() {
var image1_orig = page.getViewById('question-1-image');
var image2_orig = page.getViewById('question-2-image');
image1_orig.src = imageSRC_1;
image2_orig.src = imageSRC_2;
animateInImages();
}
}
start Instruments and track the memory usage for ImageMemoryTest app
tap on the image (this will load new iage from provided URL) - result the cache will not be cleaned which will lead to Out of Memory. Using nativescript-web-image-cache will resolve this issue)
I've just tested on Android and the same memory issue exists.
Using the image-cache plugin does seem to work but I can't now use the technique above to ensure both images are loaded before displaying. I don't suppose you have any ideas about this issue I've just opened? VideoSpike/nativescript-web-image-cache#24
@3rror404 for your scenario you can use and manually trigger the garbage collector before loading the new images.
Here is how to do it in iOS:
function loadQuestionImages(question) {
if (app.ios) {
global.__collect();
}
// the loading of the new images follows here
}
After the GC is called the memory usage no longer is rising on each tap and is at stable level.
(using Instruments >> Allocations for tracking the memory consumption)
You can also trigger GC for Android - look here for more details
@3rror404 use this only as a last resort and where needed as it might lead to side effects (mainly with page transitions) and is not the recommended way to handle memory usage in iOS app (that is the reason the method is private). Meanwhile, we will investigate the reasons for this and post additional info here.
The idea of this app is for the user to choose an image quickly so I'm switching images a lot. Maybe 30-40 times in 10-20 seconds or less dependant on network speed.
Could triggering the GC in rapid succession cause any problems?
Would it be better to trigger the GC every few image changes rather than every time?
What side effects might this cause?
One of the thing that might happen is to clear reference to objects that are still needed (for example object needed for navigation). .I can not answer your question directly - it is best to test the scenario when you are using the GC and then navigation to another page and back - and if there is no low performance in the page transitions then you should be ok.
Hi @3rror404,
We tested the provided project in your very first comment, and we are not able to recreate this issue while using latest tns-core-modules and tns-ios (3.4.1).
If you still facing the issue, please provide steps for reproducing, and I will reopen the issue.
Activity
NickIliev commentedon Mar 28, 2017
Hey @3rror404 thank you for reporting this one - indeed the memory consumption is rising and sometimes the app is crashing or freezing due to out of memory before the cache is cleared. We will look into this. Meanwhile, you can use nativescript-web-image-cache to load your online images.
With this plugin the memory consumption is normal but if needed you can force clean the cache as well.
e.g. (based on your test application)
main-page.js
main-page.xml
App to reproduce this issue here
Steps to reproduce:
ImageMemoryTestapp3rror404 commentedon Mar 30, 2017
Thanks @NickIliev,
I've just tested on Android and the same memory issue exists.
Using the image-cache plugin does seem to work but I can't now use the technique above to ensure both images are loaded before displaying. I don't suppose you have any ideas about this issue I've just opened? VideoSpike/nativescript-web-image-cache#24
NickIliev commentedon Mar 30, 2017
@3rror404 for your scenario you can use
and manually trigger the garbage collector before loading the new images.
Here is how to do it in iOS:
After the GC is called the memory usage no longer is rising on each tap and is at stable level.
(using Instruments >> Allocations for tracking the memory consumption)
You can also trigger GC for Android - look here for more details
3rror404 commentedon Mar 30, 2017
That works great. Thanks for the help.
NickIliev commentedon Mar 31, 2017
@3rror404 use this only as a last resort and where needed as it might lead to side effects (mainly with page transitions) and is not the recommended way to handle memory usage in iOS app (that is the reason the method is private). Meanwhile, we will investigate the reasons for this and post additional info here.
3rror404 commentedon Mar 31, 2017
Ok cool.
The idea of this app is for the user to choose an image quickly so I'm switching images a lot. Maybe 30-40 times in 10-20 seconds or less dependant on network speed.
Could triggering the GC in rapid succession cause any problems?
Would it be better to trigger the GC every few image changes rather than every time?
What side effects might this cause?
NickIliev commentedon Apr 3, 2017
One of the thing that might happen is to clear reference to objects that are still needed (for example object needed for navigation). .I can not answer your question directly - it is best to test the scenario when you are using the GC and then navigation to another page and back - and if there is no low performance in the page transitions then you should be ok.
tsonevn commentedon Feb 21, 2018
Hi @3rror404,
We tested the provided project in your very first comment, and we are not able to recreate this issue while using latest tns-core-modules and tns-ios (3.4.1).
If you still facing the issue, please provide steps for reproducing, and I will reopen the issue.
lock commentedon Aug 27, 2019
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.