Registry Mirror
Skip to content

Navigation memory usage increases on subsequent navigations [iOS] #6890

Closed as not planned

Description

@NickIliev

From @NL33 on February 5, 2019 23:7

Edit: Below I describe a bug that I assumed was an issue with the emulator. That might be the case, but alternatively could there be a navigation bug?

In my case, the emulator had been open a long time, and the pages where the issue appeared had experienced a lot of back and forth navigation. Is it possible that the navigation "fills up" (maybe the cache gets full), and this can lead to strange navigation behavior?

In my case, after many many (over a hundred) back and forths on the page, the navigation button just stopped working and the page would automatically navigate back to an earlier page (without any navigation being ordered). Maybe this was because of an over-used navigation?

If not, then it is probably an emulator bug as described below:


Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 5.1.1
  • Cross-platform modules: 5.1.1
  • Android Runtime: 5.1.0
  • iOS Runtime: 5.1.1

Describe the bug
I was running the emulator (ios, iPad) for approximately a day and half (ie, starting the emulator and not stopping it for a day and half; the computer was periodically asleep during the time, but the emulator was not shut down.). At that point, it started having faulty router navigation.

Specifically, (i) a navigation button would stop working and (ii) the emulator would navigate away from a certain page immediately upon going back to that page (even though navigation had not been ordered).

Navigation on the app is done with Router, Navigation Extras, and Active Router.

It is very unlikely this is a code error, because I have run the same code multiple times on different emulators (and on my device), without seeing this behavior.

I closed the emulator. On running it again, the behavior has stopped (and navigation is working properly).

So it seems likely to me that the navigation "glitch" was specific to the emulator, and probably a result of the emulator running for so long.

A breakdown in the emulator after running it for so long is not a significant concern. However, it would be helpful to know whether others have seen buggy behavior in an emulator open for a long time, as it would help me confirm whether or not there is some hidden code problem, a bug in router with Nativescript, or an issue with the iOS back button.

To Reproduce
I was running the emulator (using a webpack bundle), focusing on ipad. So I ran:
$ tns run ios --device "iPad Pro (12.9-inch) (2nd generation)" --bundle --env.development --env.uglify --env.aot

The emulator ran for about a day and a half (ie, 36 hrs+) before the behavior began. The computer was asleep periodically during the time, but the emulator was not closed.

Copied from original issue: NativeScript/nativescript-cli#4340


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Activity

  1. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    From @KristianDD on February 8, 2019 12:13

    Hi @NL33 ,
    We haven't observed such behavior while developing with the framework on a simulator. The simulator is just a common process running in the operating system and its state is not guaranteed to be properly preserved during sleep/wake cycles.

    We recommend that you stop the previous tns run session and start a fresh one.

    We are also not aware of any issues with multiple back and forth navigation and also have an automated test that verifies this functionality. Did you experience this issue on a fresh simulator instance within a single tns run session, without wake/awake of the computer? If so there might be a memory leak caused by the code of your application. You can verify that by trying to reproduce the issue with a blank angular project with drawer/tab navigation.

  2. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    From @NL33 on February 8, 2019 13:57

    Thank you, @KristianDD. When I stopped the session and started a new one, the problem went away. My "best" guess is that maybe there is some memory leakage, but just a little, and it takes a long time to build up, so you would only experience it after a long time?

    In case that is the issue, I have put clearHistory="true" on the key navigations that were causing issues. I am thinking this is the key way to combat memory leaks. Does that sound right?

  3. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    From @NL33 on February 8, 2019 22:26

    As a bit more info, it does seem that memory use on the app is steadily increasing as the app is used (at least on the emulator). With each navigation, memory use grows. So perhaps that was the issue. I had hoped that putting clearHistory="true" would reset the memory to avoid this problem. But xcode shows memory use increasing with each navigation even with clearHistory.

    Is there something else I need to be doing? I have researched possible causes of memory leaks and cannot see what might be happening here, other than if NS 5.1+webpack for iOS has small memory leaks all the time. It would be great if there is something I can run periodically in the app to lower memory usage.

    I have tried garbage collection, but that did not seem to make the difference. If the app was at, say, 285 mbs before running GC, it stayed there after, and would slowly increase with further navigation.

    Is there anything I can do to reset the memory in the app while it is running?

  4. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    Moving this issue to the modules repository as it seems related more to a memory leak than to a CLI issue.

    @NL33 if possible please provide us with a sample demo app which we could use to investigate the case. It seems that you are using an Angular based app but are you having issues on specific pages or on each navigation? Are you using page transitions and is the issue only happening on iOS or on Android? Posting a simple demo that reproduces the issue will greatly help in understanding the cause.

  5. NL33 commented on Feb 12, 2019

    @NL33

    Thank you, @NickIliev for following up. The app on which this is occurring is fairly involved but I will see if I can create a different app and reproduce it. My app is an Angular app, currently focused on iOS, with the specs noted above (NS 5.1).

    I have further detail below. The key question I am facing: is there a way to restart / clear out memory use of an app? So, if an app starts at 50mbs, and then rises through loading some heavier pages to 100mbs, is there a way to get the memory use back down to 50mbs when the user is no longer using those resources and returns to a basic page?

    I have tried clearHistory: true between pages. And I have tried activating garbage collection. These do not seem to make a difference (I do not notice a memory drop when they are used). I am not subscribing to any rxjs events (unless that is something that NS does in the background).

    Further Detail:

    I confess I am not aware of what the expected memory performance would be. The memory issue is roughly the same whether on iphone, ipad, or emulator--so it seems it is not a simulator issue. I am measuring memory use by plugging in my device, running the app through xcode, and tracking memory use on xcode.

    Memory use increases as the app is used. In navigating between pages, the back button does seem to help things--so that it may take memory use back down a few mbs. But overall memory use rises. For instance, in my last test on an iphone, my app loaded at about 50mbs. after a few minutes of use it rose to about 80mbs, and then I tested some heavy memory pages, and it went up to 100mbs.

    Despite navigating back to pages with very little happening (simple component structure and little data used), memory never fell much below that. Once it hit a level (in this case, 100mbs), it stays around that level, and then rises again for heavier pages.

    My hope would be there is a way the app can clear out the memory after it is no longer uses that memory. So if the app loads at 50mbs, there would be a way to clear things out to get it back down to 50mbs when I am back on, say, the home page.

    Is that possible?

    Or is it to be expected that memory use gradually rises as the app is used, and once it hits a certain level (for example 100mbs) it will not fall back to its original level (like 50mbs)?

    One last key note: Based on advice I found to help memory performance, I added enableProd() to main.ts. This may have helped a little. I was told to add this to main.aot.ts as well, but this file is NO LONGER created in my app after upgrading to 5.1.

    Further, I see no clear reference in webpack.config.js that aot is present, despite doing the normal command: $ tns run ios --build --env.aot... Maybe this could be related?

  6. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    @NL33 here you can find the article explaining how to use some memory management techniques. However in the common case, you should not experience any memory issues on navigation (make sure that you are testing on a real device because on some emulators/simulators there will be no memory pressure and the memory won't be released while on a real device the memory will be released in a different manner).

    Waiting for your sample app so we could test on our side.

  7. NL33 commented on Feb 12, 2019

    @NL33

    Thanks. I will investigate a sample app later on today (I'll have to create a new app). In the normal case, would you expect that an app's memory use will go to the starting point when it is returned to the starting page?

    So, in my case if my app starts at 50mbs, and then through use for several minutes and some heavy pages rises to 100mbs, would you expect it should fall back to about 50 mbs after it has returned to the home page?

  8. NickIliev commented on Feb 12, 2019

    @NickIliev
    ContributorAuthor

    @NL33 is the leak happening on a real device or emulator? Is this Android or iOS? In all cases, on a real device, the memory should remain on some stable levels and unused objects should be released and cleared by the GC.

  9. NL33 commented on Feb 12, 2019

    @NL33
    1. iOS focused. 2. happening on real devices (and emulator too).

    Is there a danger in calling garbage collection? I have it set up to activate whenever I get an ios memory warning, but I am inclined to try calling it whenever I return to the home page as well. I am fine losing the navigation history.

    I also have the strange result that in any NS app I create now (using NS 5.1), when I then call $ tns run ios bundle env.uglify env.aot... , trying to get webpack+uglify+aot, I do not see any aot file being created, and no statement in webpack.config.js that there is aot. Should there be a main.aot.ts file in the most recent Nativescript? If so, maybe the fact that it is not there for me is related.

  10. NL33 commented on Feb 13, 2019

    @NL33

    @NickIliev I have created a simple test app at https://github.com/NL33/memory-test-app.

    Description is in the readme. Essentially:

    I created a webpack bundle ($ tns build ios --bundle --env.uglify --env.aot) and then also ran webpack ($ tns run ios --bundle --env.uglify --env.aot). I then tested the app in xcode, running on a plugged in iphone.

    Using the app to test I try to build up memory use by going to the video page, and from there going to the list page, making a bunch of selections, then viewing the selections, and scrolling up and down. This tends to build up the memory use. I then go back to the home page, and repeat.

    I did this a few times, and then waited a few minutes, and did a few times more.

    The app went from about 45 mbs on load to, after a few minutes of the steps above, 90mbs. Returning to the home page would get the memory footprint down a little each time, but never all the way back. Once I hit 90 mbs it never went below about 75mbs on return to home page, despite the home page clearing out all local storage.

    This app is simpler than the app I am working on, so the memory swings are less pronounced. But this app seems similar in that the general trend appears to be an increasing memory footprint as the app is used.

    Note that, despite running env.aot in the bundle command, there is no main.aot.ts.

    I look forward to hearing your view on whether this performance is normal and if not what can be done.

  11. 26 remaining items

  12. NL33 commented on Feb 25, 2019

    @NL33

    On my main app, I do also get this warning all over the place (the console repeats this about 30 times when the app loads):

    CONSOLE WARN file:///app/vendor.js:[1:111....]: Objective-C class name "t" is already in use - using "t28" instead.

    I assume that's not related.

  13. NickIliev commented on Feb 26, 2019

    @NickIliev
    ContributorAuthor

    @NL33 yep I meant the article approach as there are some additional optimizations behind those flags (e.g. like calling GC multiple times in different VM passes).

    I would also like to notice that the garbage collectior might not be called immedialty. In fact on real device, the unused objects might be releaed after memory pressure so you don't ahve ot worry about increasing mmemory unless of course this is leading to OOM. Still, thats highly unlikely and the iOS will usually free the memory once there is enough memory pressure. If you want to understand which objects exactly are the ones that are causing the memry increase then refer to the techniques described here.

  14. NL33 commented on Feb 26, 2019

    @NL33

    Thanks. That is helpful to hear--If I understand what you are saying, it is that even if memory use increases steadily (going from 40mbs to 100mbs after several minutes, and likely up from there...), that iOS has built in safeguards to generally address the concern.

    If I could give one more shot at my main question:

    When I return to the simple home page, nothing else is happening in the app other than displaying that simple text.

    When I am on the home page, is there something I can do to be sure I have cleared out all objects and views so I am returning to the starting memory point?

    My hope is that if I start at 40mbs, and go to 100+ after navigating through heavy pages, each time I return to the home page I can go back to close to 40mbs. The only advice I am seeing--like in the article you referenced--is to call GC(). Unless I am doing it wrong (see sample code above), that does not seem to make a difference in clearing everything out. I am hoping there is something else I can do.

  15. NL33 commented on Mar 5, 2019

    @NL33

    Hi @NickIliev, thought I'd give one more shot at the key question I have:

    When I am on the home page, is there anything I can do to be sure I have cleared out all objects and views so I am returning to the starting memory point?

    Thank you.

  16. NL33 commented on Mar 8, 2019

    @NL33

    @NickIliev and @NathanaelA: I put together a sample app that I think shows the memory problem. Essentially, I beefed up the old sample app. (I also use this app to test RadListView performance, but you can ignore that for these purposes).

    Here's the app: https://github.com/NL33/NSExampleApp_MemTest .

    I run it with webpack ($ tns run ios --bundle --env.uglify --env.aot)

    After using the app for about 5 minutes (on real device--iPhone SE running iOS 11), I find memory use increasing steadily. Returning to the home page never gets this back close to the start, even though the app clears out local storage on the home page. The home page lowers the memory use by about 4-5 mbs when I return to it, but that doesn't help much if the other pages are adding 20, 30, 40+ mbs on load.

    So, the question: What can I do on the home page to get things back close to the starting memory point?

    Without that solution, memory use just goes up and up.

    Here's further detail:

    Stats:

    Nativescript 5.2.0
    TNS core modules  5.2.2-rc-2019-03-01-112723-03
    TNS-ios 5.2.0
    

    The app has a series of lists that increase memory use. To test, click the red button on the home page, then scroll around, and then go to the next list, and then the next. I find memory use is relatively stable for the first few minutes, but then about minute 4 or 5, things just start going up and up.

    Here are some screen shots of the state of memory use on the home page:

    1) Home page on Load: (about 54 mbs):

    1_home_page_onload

    2) Home page 6 min, 56 secs in:

    2_home_page_6min56sec

    3) Home page 8 mins, 46 secs in:

    4_home_page_8min46sec

    4) Home page 9 mins, 46 secs in:

    3_home_page_9min46sec

    This seems to me to be unsustainable. This is still a simple app, and the pages are not especially heavy. And yet memory continues to increase while the app is used.

  17. NickIliev commented on Mar 11, 2019

    @NickIliev
    ContributorAuthor

    @NL33 I've built your app but the memory is pretty stable and I am not seeing these memory spikes even after long usage. However, there are some newly introduced iOS custom flags (they were previously supported only on Android) which allows you to trigger the GC on a specific interval which is very useful for apps where you want more control over the memory management. Try setting a GC throttle interval or a memory check interval (the differences are explained in the linked documentation article)

    One more thing- the latest app seems to have a memory issue with RadListView more than with the navigation - as far as I understood the issue is manifesting when you are navigating from a page with RLV rather than from any page. If that is the case please consider moving the issue for RadListView in the UI feedback repository so we could track each problem in the related repository.

  18. NL33 commented on Mar 11, 2019

    @NL33

    Thanks for taking a look. The behavior in the screen shots is pretty consistent for me--going from 50 mbs to over 100mbs on the home page after about 8 or 9 minutes.

    1. Is there anything else I can do to clear away memory (especially from past navigation) on the home page?

    2. Are you not seeing this behavior? I am running the app through xCode on a phone with iOS 11. Are you using something else?

    Thanks. BTW, I do not find there is something especially bad about RadListView from a memory perspective. Rather, I set up the app to show some other glitches in RadListView performance, and they are the basis of a separate report already.

  19. NL33 commented on Mar 18, 2019

    @NL33

    Bump. @NickIliev:

    1. Is there anything else I can do to clear away memory (especially from past navigation) on the home page?

    2. Are you seeing a different memory performance than I document above?

  20. NL33 commented on Apr 2, 2019

    @NL33

    Hi @NickIliev, any thoughts on my earlier questions:

    1. Is there anything else I can do to clear away memory (especially from past navigation) on the home page?

    2. Are you seeing a different memory performance than I document above?

  21. NickIliev commented on Apr 3, 2019

    @NickIliev
    ContributorAuthor

    @NL33 no I do not see this while using latest XCode and iPhone X.

    About your 1. question

    Is there anything else I can do to clear away memory (especially from past navigation) on the home page?

    I've answered before you asked in your last three comments - Have you tried the GC flags that I've linked in this comment?

    @NL33 I've built your app but the memory is pretty stable, and I do not see these memory spikes even after long usage. However, there are some newly introduced iOS custom flags (they were previously supported only on Android) which allows you to trigger the GC on a specific interval which is very useful for apps where you want more control over the memory management. Try setting a GC throttle interval or a memory check interval (the differences are explained in the linked documentation article)

    Have you tried those options n your application? If testing on a real device then notice that the iOS will free memory on memory pressure which is somehow hard to achieve on newer devices. Is your device experiencing memory issues (like crashes & OOM errors)?

  22. NL33 commented on Apr 3, 2019

    @NL33

    Thank you. The screenshots were taken using an iPhone SE with iOS 11.4 (or perhaps 12.1). This seems to me a fair approximation of the majority of users. My device does not experience memory issues. Instead, I am just concerned that memory use seems to increase steadily.

    I take it from your comments here that, in the case that memory pressure becomes too great, iOS will NOT close the app; instead, it will take other steps to free up memory pressure.

    As a result, I interpret your comments to mean that memory performance here should not be a concern.

    Regarding the flags you note: The memory performance I show here does not change, even when calling garbage collection EVERY TIME the user returns to the home page. So I have put the following on the home page:

    import { GC } from "utils/utils";
    ...
    ngOnInit(){
      this.page.on('navigatedTo', (data) => {
                GC()
       })
    }
    

    And it has not made any difference in memory performance. My guess is that view components are building up in the app, and GC does not remove them

  23. MartoYankov commented on Apr 5, 2019

    @MartoYankov
    Contributor

    Hi @NL33

    I'd like to jump in and try to address your concerns.

    1. Having higher memory usage in an app after it has been used for some time, does not mean there is a memory issue by itself. The GC process on iOS doesn't do anything if there is no memory pressure and memory pressure isn't easy to simulate. Even the iPhone SE has 2GB of RAM. On your screenshots you can see Usage Comparison, that your app takes far less memory compared to other processes on the device. Even if your app starts to use more memory, the OS will try to dispose other processes first. The easiest way to put memory pressure and force GC is to use large images in your app.

    2. Having excess memory after GC has been triggered also does not mean that there is memory issue by itself. If I'm not mistaken, the graph you see in Xcode displays allocated memory. It's possible that for performance reasons, the OS allocated more memory than it needed, for example for a collection. Even if the items in this collection are disposed, the collection and its allocated memory can be kept, because you might want to go forward again.

    3. The above two points are actually educated guesses, because iOS isn't open source and I imagine GC even works differently on different iOS versions.

    4. If you have concerns there is a memory issue or a leak, the best way to go about it is to automate the scenario in your app, run it in a loop a thousand times and observe the memory graph. If the app crashes with OOM, then there is an issue. If the memory consumption plateaus at some point, this is expected. The OS has decided this is the memory it can give you.

    5. In general NativeScript and similar technologies have a bit higher memory consumption than native, but have the added benefit of being cross platform and letting you use web technologies.

    Hope this makes sense.

  24. NL33 commented on Apr 6, 2019

    @NL33

    @MartoYankov Thank you very much for this thorough explanation. I am hoping it also helps others who may have questions about memory use. I will consider running a loop to test. In the meantime, thanks again to you and @NickIliev.

  25. NathanWalker commented on Oct 23, 2026

    @NathanWalker
    Contributor

    Closing as not a leak, per the maintainers' analysis above: memory rising during long sessions without memory pressure is normal GC behavior on iOS, and memory was stable under repeated navigation. Please open a new issue with an Instruments trace if memory grows without bound on current @nativescript/core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions