Search Unity

Performance gets worse after a while of playing

Discussion in 'Android' started by Ljaljevic1120, Mar 11, 2020.

  1. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I'm making a 2D Android game that I plan on releasing.

    It's having this issue where after playing a certain amount of levels (about 12), it starts to lag dramatically. It lags to the point where the game becomes unplayable and eventually completely freezes!
    When the game is closed and reopened, everything is back to normal and it goes through this whole cycle again.

    I feel like I've tried everything to optimize the game as much as possible, but this is the only thing holding me back from releasing it.

    Here is a screenshot of the profiler in the middle of the bad lagging. I just don't even know what is going on here, my game implements a very small amount of physics...

    WB Profiler Screen Shot.png
     
  2. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Do you have all the 12 levels statically built into the game? If you do, maybe consider loading them one by one from a server, that way you dont have all 12 scenes in there at once, its what im doing, maybe that will help? Also loading from a server you can have infinite levels, or update existing levels without making a new build. But of course its more complicated.
     
  3. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Hi, thanks for the reply!

    I'm making a simple offline game that will have a max of 20 different levels, so I'm not sure about the whole loading from a server thing.

    I should have mentioned that I only have 3 out of the 20 levels completed. It's when I keep replaying those 3 levels that the game starts to lag, and the lag starts to become apparent after playing those levels about 12 times.
     
  4. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Try using the profiler, see which memory is not being released.
     
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    That is... Quite some overhead for collecting memory stats so yes, my first guess would also be that there's something funky going on with your memory usage. Try monitoring the memory Profiler chart in the Profiler and taking memory snapshot with the memory Profiler package at different points in time (e.g. beginning of a level, end of level, first time you play a level, repeat play of a level) and make a diff between these to see how memory usage changes and if that's expected.
     
  6. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    So far I've found one game object that could be causing some memory leakage. I will be doing more testing to check for more problems.

    I just don't know how to go about fixing the issue. Should I change something in my code?

    The game object I found is a simple fade-in and fade-out animation used to transition between scenes. The animation is a UI element that covers the entire screen, and it's always disabled. It's only enabled when it's time to transition.

    Thanks for the help. :)
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    You will likely have to change something in code. I don't get what kind of issue you found though.
    Are you creating a GameObject/ScriptableObject manually? If so you'd need to Destroy() them. Are you using a RenderTexture? You'll need to Release it.

    I can't tell you any more with the details I have.
     
  8. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    It's just a UI Image. It has two animations that animate the alpha color channel to create a simple fade-in and fade-out transition between scenes. It also has a script attached to it that just loads the next scene when the corresponding animation ends. Every scene contains a copy of it to achieve the transition effect.

    I followed this guide on how to use the Memory Profiler to find memory leaks in the game. I just don't know how to solve the problem after I've spotted it.

    Btw I really appreciate your help :rolleyes::)

    Screen Shot 2020.png
     
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    I still don't get what the memory Profiler hinted at that woul be leaking. Is it the fade panel GameObject? Or is it something it holds onto? Maybe something to do with the levels it's loading?
     
  10. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Looks like it could be a component attached to the fade panel according to the Memory Profiler.
    This is my first time profiling memory in-depth, so bear with me as I learn...

    Screen Shot 2020-03-16 at 9.25.55 PM.png
     
  11. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    Did you find that by pressing the diff button? How did you come to the conclusion that this is the problem? I feel like I'm missing context here. Have you had a look at this guide?
     
  12. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Ok, let's get the confusion out of the way...

    I've read the guide you mentioned and I've also watched this tutorial on YouTube. Now I have a much better understanding of what's happening within my game, and I will explain.

    I did some more testing and capturing of the memory in my game. Forget the things that I said before with the fade panel because I have some updated info. I have a "MainMenu" scene, which is only UI, that the game is mainly controlled from. The user uses the UI to select levels to play. For example, when level one is selected. The "LVL1" scene is loaded. When the user completes the level, it loads the "MainMenu" scene back again. This is how the whole game operates, load into levels and play them, over and over again.

    I took a capture of the "MainMenu" scene at the start of the game. Then I took another capture after playing for a while. I opened the captures and pressed the diff button. Then I ordered the list like this:

    Screen Shot 01.png

    After that, I opened the "New" dropdown.

    Screen Shot 02.png
    If we look at "Ball 1 Btn" (UI element), we can see that it was created 10 times. So then I opened the "Deleted" dropdown.

    Screen Shot 03.png

    Here we can see that the same "Ball 1 Btn" object was deleted only 5 times. From what I can understand, 10 instances of the object where created in total, but only 5 of those 10 were actually deleted.

    Now correct me if I'm wrong, but this is what I think is actually killing the performance of my game. :oops:
     
  13. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    Not just the ball UI. Looks like you leaked 6 canvases and (5 times) their content. And now I can see the Fade Panel also being part of that. Seems like your ingame UI might be dangling around.
    Good find :)
    And happy to see that the tool helped discover this. In case you haven't already done so or don't already know why that's still around, for the next steps you could check the newer snapshot, search for that UI and hunt down what references it. Maybe you registered something to some static events and didn't unregister?
     
  14. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Now we're getting somewhere!

    So I did what you said and opened the newer snapshot. As a good starting point, I looked at the "Ball 1 Btn" and clicked on its RefCount number. This is what I got:

    Screen Shot 2020-03-18 at 10.47.41 AM.png

    This is where I need help because it's my first time doing this. :confused:

    Also wanted to ask, what does it mean to register something to a static event?
     
  15. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    Now this is a bit of the sad part where the Memory Profiler doesn't yet calculate the PathToRoot (the thing that is ultimately holding it in memory) so you'll have to step through the references to find what's holding it. As I said about, it's likely one of the two canvases here being held by something else, so go to it's references and keep going. If that ends up turning you around in a circle, I guess it's something else. If you get turned around too much, maybe HeapExplorer can help.

    It doesn't have to be static but could just be on something that hasn't been collected yet but essentially this:
    Code (CSharp):
    1. //somewhere
    2. public static event Action onSomethingHappened;
    3.  
    4. //Elsewhere
    5. onSomethingHappened += MyMethod;
     
  16. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Ok, so I made some changes to my code and I was able to completely fix the problem with the UI not being deleted. I tested my game some more, and it runs much better than before.

    I still do have some bad lagging though...

    Looking at the many memory captures I've taken, all of my objects are being deleted correctly in all of my scenes.

    Not exactly sure what to look for?
     
  17. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Is the device warm when you do performance measurements?
    Mobile devices have to reduce the CPU and GPU clocks to let them cool down. This will have an effect on performance, so a good rule of thumb is to have it rest for at least 5-10 minutes before measuring performance.
     
  18. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    I'm guessing it shouldn't be down to memory Profiler stats collection anymore though, right? Maybe it is as Aleksandr said down to thermal throttling but without new CPU profiler measurements it's hard to tell ;)
     
  19. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Thanks for the support guys :)
    I ran a bunch of more tests and just wanted to share them with you.

    So after about 10 mins of playing the game (20 levels), I've looked at the memory for all of the different scenes. I couldn't find anything outstanding. To my eye, everything looks a-okay. I now turned to the CPU Profiler to analyze the behaviors of the game during its moments of lagging. These screenshots I took where of particular moments when the framerate would drop to about 5-10 fps for just 2-3 seconds:

    Screen Shot 2020-03-19 at 9.51.18 PM.png
    Screen Shot 2020-03-19 at 10.04.32 PM.png
    Screen Shot 2020-03-19 at 10.05.22 PM.png

    And, for some reason, I get the occasional weird spike:

    Screen Shot 2020-03-19 at 10.08.24 PM.png

    I'm not sure if this is still related to memory, but please let me know what you guys can make of this. By the way, I made sure the phone was not heating up between tests, in fact, the phone only gets slightly warm when I play through the game.
     
    Last edited: Mar 20, 2020
  20. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    This "RenderTexture.SetActive" taking 30 ms on the second screenshot looks suspicious :)
    On the third screenshot it looks like there's a bug with "Wait for target FPS", as, I guess, it's set to 30 in your case, and the frame takes longer than that. We shouldn't see it in the profiler at all.

    Which Unity version are you on?
     
  21. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I'm on the latest Unity version 2019.3.6f1

    I've actually sent a bug report to Unity about the "Wait for target FPS" thing about a month ago now. They got back to me saying that they were able to reproduce the bug and that they have sent it to their developers to be fixed. I always check the release notes for every new Unity version to see if they have fixed the bug, but it still remains unfixed at the moment.
     
  22. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
  23. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Yep, looks like it...

    So does it need more votes to be fixed or something?
     
    Last edited: Mar 21, 2020
  24. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    More votes is not necessary, we'll take a look at this soon :)
     
  25. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Awesome! Fixing this bug would be a big relief for my current and future projects.

    I did want to ask what you guys thought about the screenshots I posted in the previous thread? By that I mean, what do you guys think is the cause of these weird spikes in performance?

    One thing I noticed is that I use Google Admob ads in my game. I have a rewarded video that grants the player more diamonds. When I play the game for a while and it starts to lag, the rewarded video no longer displays videos nor does it reward the player.

    Strange, but I can't seem to get if this is still a memory problem or something else...
     
  26. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Also just some more screenshots of the game in action:

    Screen Shot 2020-03-21 at 11.52.01 AM.png Screen Shot 2020-03-21 at 11.54.32 AM.png
     
  27. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Well, I said what I had to say about the screenshots already :)

    Try the command `adb shell dumpsys <your_app>`, it will say, how much memory your app is using from the point of view of the OS.
     
  28. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    This is interesting. My project doesn't have any render textures in it.
     
  29. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    It could be related to AdMob? What happens if you disable that?
     
  30. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Just disabled all Admob ads and tested it. Wow, the game performed flawlessly!

    I had no idea that ads, of all things, could have such an impact on performance. I only have 3 levels built, but I played those 3 levels about 35 times over again (simulating what users will do). There was not even one moment of lag.

    I guess now the question is, why does Admob do this to the performance of my game???
     
    aleksandrk likes this.
  31. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    I sadly can't answer this but you're not the first person to have stumbled over this that I'm aware of. So far I've only seen this in connection to AdMob, so maybe that's a good reason to use a different Ads provider. Maybe Unity Ads? ;)
     
    xliquid likes this.
  32. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Quick update:

    I've narrowed down what causes Admob to slow down game performance. I use banner ads that stay on the screen for the entire duration of the game. The way I had it coded was every time a level ended, it requested a new banner ad. I reworked the script so that the banner ad is requested only once at the start of the game.

    For the most part, this solves the issue. Interestingly though, every once in awhile the banner ad automatically requests a different ad to display, and every time it does, the game lags for that brief second then returns to normal.

    Still looking into why it does that, and if it's fixable...

    By the way, I don't plan on using a different ad provider because I'm already too deep into switching at this point (if you know what I mean).
     
  33. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    yeah, I guess it makes sense that downloading a different banner would have performance implications. Also hear your point about switching, though I personally think banner ads are a bit to intrusive for the little value they provide, vs incentivized video ads. But that obviously doesn't always work for every kind of game.
     
  34. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I just made some further optimizations to the ads, and after testing, the performance problems they had are close to unnoticeable.

    I agree!
    To be completely honest, my game implements banner ads, rewarded video ads, and interstitial ads because I want to increase the number of possible clicks. Ultimately resulting in slightly more revenue. :rolleyes: I want to make it clear that I didn't sacrifice the overall experience of the game just to display more ads. I think I've got a nice balance between ads and user experience.

    At this point, I've pretty much fixed the original problem I had with the game. So I think that we can officially end this long thread here. Thanks MartinTilo and aleksandrk for all the help. Couldn't have done it without you guys! :);)

    (If everything goes as planned, game will be available on Google Play within 2-3 weeks and will be called "Wall Ball")
     
    MartinTilo and aleksandrk like this.
  35. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    Very nice! Glad we could help and best of luck with the launch :)
     
    Ljaljevic1120 likes this.
  36. tosdik

    tosdik

    Joined:
    Mar 5, 2018
    Posts:
    6

    Hi , I this problem too and I just load dem only when launching app. So can u share your further optimisations, I just want to apply them too :D
     
    Hozgen90 likes this.
  37. Antox3

    Antox3

    Joined:
    May 1, 2018
    Posts:
    2
    Hi, sorry to return to the subject but I find myself facing the same problem with admob banners. Can you share the optimizations you found in addition to requesting the banner once at startup please? :)
     
    Hozgen90 likes this.