How to Navigator.push memory efficiently Flutter - performance

My app has screens that are fullscreen GridViews of NetworkImages, and every time you click an image it takes you to a new page with another GridView of images. I am running into memory issues and the app is crashing because every time I Navigator.push a new page, the images on the previous page are still held in memory, causing a memory leak. I need to keep the full history of pages, and users need to go back. Is there a way to have the images freed from memory when Navigator.push is called and have them be rebuilt when there is a Navigator.pop event? I've tried using CachedNetworkImage and OptimizedCachedImage but both cause even more memory issues.
Here is a gist displaying the issue: https://gist.github.com/Sofianel5/3b29e15024b902f6f04ce2f84598171c

Make sure you are Calling dispose function in every page
#override
void dispose(){
super.dispose();
}

Related

Contents of build method rebuilt without any setState calls

My app has a lot of API calls and image downloads, that's why i'm using Cache Manager package.
I noticed a lot of content is re downloaded and my app sends API requests without me doing anything but scrolling the screen.
I started checking and the build() method does gets rebuilt any time, but it has nothing to do with any setState() calls whatsoever!
Is there a chance the garbage collector or cache issues have something to do with that?
I get a lot of debug prints like Background concurrent copying GC freed 368(32KB) AllocSpace objects, 18(1632KB) LOS objects, 56% free, 1202KB/2MB, paused 25.610ms total 107.349ms.
If not what can cause it?
Well i deleted the question but thought about it and un-deleted it to answer with the solution if anyone else will ever encounter such a behavior:
It is very weird but seems like the problem was having my widget as a child of a RefreshIndicator widget.
I'm sure i didn't even scroll to the position it should indeed activate my refresh method, but scrolling any direction too far (even horizontally while my refresh indicator was a regular vertical one) activated it for some reason.
Sorry for not providing any example while asking the question, the app was too complicated and i didn't even know what is relevant and what's not...
Edit:
Now it's just a real mystery, i added a print('Refresh') line to my onRefresh method, and it never got printed, even when the widget got rebuilt!
If anyone have some kind of explanation i would really like to hear it.

Memory allocation always increasing, despite not using my app

I am currently updating my app, and I have been facing a very strange and complex problem for the last few days. The part of the application that is problematic is made of one UITableViewController that is a list of news, and (after you click on a news) a detail view which is in fact a UICollectionView with as many details CollectionViewCells as there are news.
Each of these can have an infinite amount of elements, and are loaded 20 by 20 when the user scrolls to the bottom of the TableView (or to cell that is at the furthest position right on the CollectionView). Also, inside a DetailsCollectionViewCell, there can be another UICollectionView, containing images.
My problem is that after scrolling a few details views, after behaving normally (ie memory is allocated when I change the page, then stabilize until I change the page again, and so on), the memory allocation start to ramp up slowly but steadily, even if I stop doing anything at all. Also, the CPU usage will go to 100-120% and stay there, whatever I do, even, again, if I don't touch anything. After a while, the UICollectionView and the UITableView will not render any animation anymore, thus loosing the paging, and the inertia when scrolling, and overall resulting in a very poor user experience.
The strange thing is, I can observe these behaviours via XCode 5's Debug Navigator, but when I try to use instrument to find the source of the leaks/allocations, the allocation graph is normal, and I get 40-60 MB mem usage, no more, despite still observing the animations/paging problems.
Has anyone already met such a strange behaviour, and can someone help me in finding the cause of all this fuss ? (maybe by fixing Instruments ?)
Thank you all in advance, don't hesitate to ask me for more infos if needed !
I found the solution to my problems in the meantime.
About the difference in Memory usage between Xcode 5's Debug Navigator and Instruments, it was caused by my use of NSZombies. I have the habit of always setting them on, and that just flew off my mind... To remove them : Product > Scheme > Edit Scheme > Diagnostics > Enable Zombie Objects (just unmark it).
The cause of my CPU usage was an animation that was going on indefinitely in the background on multiple pages. The solution was to first of all stop it as soon as it is not seen/useful anymore, the optimise it by changing my approach (I was using CAAnimation and moved on to using UIView's animate function).
I think I might have pulled the trigger a bit too fast here, but hey... if this can help someone later, then it will not be a waste !

Cordova + jQuery Mobile - Much content on page = poor navigation time

I have an Cordova 3, jQM 1.3 application on iOS.
On the first page there is a lot of content. If I navigate away (through the tab/nav-bar) to the second page it displays the page really fast (under one second) because it has very few content.
Every time I navigate back to the first page it takes a long time to load it.
How can I improve that? Other applications don't seem to have such a long loading-time, even on content-heavy pages.
e: I am using changePage function, no transitions, no tap delay.
It depends on your content and your application architecture.
One solution would be to not show all the content, but just a part of it and provide some mechanism for the loading the rest (scrolling down, press a button etc). This would work on single page applications (every page corresponds to its own html file) and on multipage applications (one html file for the whole app).
Another solution is to load the page when your app is launched. This would only work on multipage applications. That would mean that the page is ready, in the DOM tree and available for use whenever you call for it. This has the downside of having a bigger DOM when it isn't actually needed, and it might slow down other aspects of your app.
Finally, try upgrading in jqm 1.4 as it is stated to be faster. Try to produce your content in an efficient way (you might already do that,so just ignore me on this one if you do). Don't output your context in parts (e.g multiple html() in a for loop). Concat big parts of your content as much as you can and output it as a whole (whenever that's possible of course).

how to solve Memory leak in wp7

I am developing one application in Silverlight for windows phone 7. I am stuck in very common issues which comes in windows phone app however not able to get out of it in any ways. It is memory leak issue which comes during navigation from first page to second, second to first and so on for multiple times.
To solve it, i create one new project having 2 blank pages. Each page has 2 text blocks to print current memory and peak memory and one button to move to next or previous page. When navigate from page 1 to page 2, i make null referance of all 3 things and call gc.collect to destroy the page referance. Same way, while moving from page 2 to page 1, i do the same thing.
I also tried to call gc.collect() in timer for every 500 mili seconds, but still no result. If i remove gc.collect() totally, memory increases in MB so i think it is a must thing.
You're doing it wrong. If you're continuously navigating from page 1 to page 2 then to page 1 again, you're keeping all the previous page instances in the navigation stack. It's bad for the memory management problems you've pointed out, but it's also awful UX as the user will have to press the back button a great deal of times before exiting the app (actually, I'm not even sure it would get past through marketplace certification).
After navigating to page 2, if you want to go to page 1 you need to call NavigationService.GoBack rather than NavigationService.Navigate. It will restore the previously cached instance of page 1 (so obviously you mustn't nullify the references on that page).
On some rare conditions, you might really want to navigate to a new instance of Page 1 instead of the previous one. In that case, call NavigationService.RemoveBackEntry to remove the latest cached page from the navigation stack (you can call that multiple times to clear the entire stack).

WP7 - Xaml Pages and Peak Memory problem

I have a problem with 90mb peak memory limit issue.
For example: I create 1 Panorama Application(with default content) and add 3 PivotPages or 6 Portrait Page(Page A and Page B.. with blank content). Each time I navigate and go back between these pages the memory usage is going higher and higher. At the end, It passes the 90mb limit.
I use buttons to navigate like this:
private void btn1_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/PageA.xaml", UriKind.Relative));
}
Do I miss something important while navigating between pages? While searching for a solution I heard about garbage collector? How can I use it or do I need to?
I couldn't find an answer for that; I found this similar topic: http://forums.create.msdn.com/forums/p/76007/466968.aspx but there isn't a final solution.
Note: I am using Coding4fun toolkit to measure memory usage.
Edit: I created a sample panorama application with some pivot pages (1 with content).
http://i54.tinypic.com/zfip.jpg
At start, application opened with 30mb. After I navigate to the same pivot page a couple of times, Peakmemory started to increase little by little and stoped at 47mb. Is this normal? Maybe, I didn't understand the dynamics of this application building stuff. But it doesn't make sense if it keeps increasing the ram usage and makes application crush after some use.
Somethign is stopping your pages from being unloaded when you navigate away from them. You probably have a resource leak somewhere but without seeing your code we will be very unlikely to be able to help more.
You shouldn't need to worry about garbage collection.

Resources