Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[#STUCK] Frames Per Second Drop very slowly from 60 - 45, can't find memory leak!!

Discussion in 'Editor & General Support' started by ClearRoseOfWar, Jul 15, 2016.

  1. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    [INTRO / ABOUT THE GAME]
    I've created a 3D game platformed for android that randomly generates level chunks as you move forward through the stage. Each chunk is 64x64x64 units, but only consists of about 16 Faces MAX per chunk. When the chunk is behind the camera, it is then Destroyed, and another randomly selected piece is Instantiated at the end of the chain, to create a loop of endless pieces until you restart. It is always the same stage loaded when restarting.

    [THE PROBLEM]
    The problem is this; In Unity, when I play the game I get a steady frame rate of ~60 fps. The Tris, batching and setpass calls are all roughly predictable. The renderthread stays at about 0.5ms - 0.7ms. The CPU likes to jump from 0.9ms to 10ms (Sometimes even as high as 30 (But thats vSync)) But that generally doesn't seem to effect the fps.

    For the record: I do not get my frame rate from the stats list in unity. I found a script on the asset store that calculates the correct amount of frames the game is running at.

    Using the profiler, I was able to narrow down things that needed to be changed to improve the overall performance of the game. Furthermore, I was also able to use the profiler to check the resources being used while the game is played. Turns out, The memory wasn't building up, but was actually pretty predictable after studying it for a short period of time.

    So I concluded there are no memory leaks in edit mode.

    Its when I build the game to my phone that the problem occurs, not within unity.

    When I build it, and install it on my phone, I get a good 60 fps until I play it for several minutes, restarting and then playing as long as I can until I have to restart again... I slowly lose 15 - 20 + frames (Haven't bothered trying to get it any lower) and this is happening randomly. Its not at certain points that can be noted (and if it is then its not easy to track down). It happens from just playing for a short or long period of time, restarting...

    If I close the app (and all other open apps on my phone) Then clear the cache and data, and restart it - The fps is still at ~45fps

    If I build and reinstall, the fps is back to 60 and it happens all over again.

    [CONCLUSION]
    I don't want to move on and end up burying this potential memory leak or whatever it is and find myself working harder to solve it later..

    I'm basically looking for any and all possible suggestions (Especially from the developers if possible) in hopes to get some kind of a lead on this problem.

    If you need any more information, please ask and I apologize in advance for not already including it :)

    Thank you for your time.
    D. Lively


    EDIT:
    I also wanted to mention that this problem has been ongoing for the last month or so. At first I thought it was just faulty game design... But I've really polished things off, and there really shouldn't be any reason for this to be happening... but maybe I'm over looking something so simple...
     
    Last edited: Jul 15, 2016
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,876
    I would build it for the PC, and see if the same issue happens on the PC.
    If it the same thing happens for a PC build, then I would think it is most likely an issue with the game.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    You can also profile the android device remotely - ie. profile what's going on on the build. There's some details on this page.

    Edit: oh, and if the framerate drop persists through a device restart, it's probably something you're doing, not a memory leak.
     
  4. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    @TTTTTa I will give that a shot. Definitely something I haven't thought of trying yet. Will let you know my results asap.

    @Baste Thanks for that bit of info dude. I knew I could do that somehow based on what I'd seen in the profiler (the android profiler button) Gunna give that a shot too. I really hope its not something I'm doing cause there really shouldn't be anything thats doing this.. Especially now since I've done hours and hours of and hours of optimization to try and solve this... Not sure what it could be.

    Any more suggestions, anyone?
     
  5. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    @TTTTTa I really appreciate your help, but I'm not sure if I want to take your route just yet. I switched platforms and it tried to convert things away from android -- So your suggestion will be tried when I'm at my wits end.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    The switch isn't permanent, and doesn't do any changes to your main assets. So it's safe to switch one way and then back again.

    You can always create a copy of your project and set the copy to PC too, if the import time is too long.
     
  7. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Okay, Ill give that a try too. The odd thing is I can't get it to happen when I want. It just happens randomly when it wants to. I stopped playing for a bit and turned it back on (not a fresh build) to find the fps back at 59.88
     
  8. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    @Baste Alright! I think I am on to something now! So I have 4 scripts that have a public static component each, but the references were not being set to null OnDestroy and thus creating a memory leak, right? So I just fixed my scripts and will be doing a test after I am done writing this.

    But first I have a question. But to ask that question I must first enlighten you.

    I created a singleton which is one of the static scripts. It looks like so:

    //////////////////////////////////////////////////////////////////////////////////////////
    static public GameStartedManager theSingleton;

    void Start ()
    {
    //Only allow one of these singletons
    if (theSingleton == null)
    {
    SceneManager.LoadScene(GameManager.controller.STAGENAME);
    DontDestroyOnLoad(gameObject);
    theSingleton = this;
    }
    else if (theSingleton != this)
    {
    Debug.Log("Destroying");
    Destroy(gameObject);
    }

    }
    //////////////////////////////////////////////////////////////////////////////////////////

    when I run the the above script in play mode, I always get the debug Log 'Destroying' (Just after I pressed play) I assume it didn't say that the first time I started the game without 'theSingleton'. Does this mean that the singleton is store in the memory somewhere outside of play mode, and is already referenced when unity starts back up?

    If this is true, I may have found my memory leak.

    So, unless these static components are removed when a fresh build is applied (Keeping in mind that I have 3 other static scripts, which haven't been deleted.. EVER) I likely have a ton of them just sitting somewhere (If that's even how it works)

    So I guess my question is:
    • Are these static references just piling up somewhere?
    • Where can I find them / remove them?
    • And how do I destroy the singleton when the player is done playing the game (when the app is closed) So that Its not keeping the singleton in the ram (It doesn't need to, really[Unless its not a big deal in this instance, which I think I read somewhere])
    • I may have more questions shortly
     
    ArachnidAnimal likes this.
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    When you close your application, the OS should free all resources the application uses. That includes everything - from objects in memory to file hooks to whatever.

    Are you closing the application, or just minimizing it?
     
  10. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    I fully close the app, I think. When I press my active apps button on my phone, I 'Swipe Close' It.
     
  11. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    @Baste What if im just restarting the game (re-load the scene)... it doens't clear its memory then right? So If im not setting the static components to null, does it not keep that information in the memory until the app is closed (following your advice) which could explain why the fps is dropping, but resets after the app is closed..

    However, when I close the app, the fps doesn't always go back to 60...
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    if you reload the level, the object will be destroyed, and a reference to it won't be kept. This means that it'll be destroyed on the c++ side, not get any calls on the C# side, and eventually be GC'd.

    A single (or even many) game managers waiting for GC should not slow down your application. They're not doing anything, and if you run out of memory, the GC will clean those objects away. The only way you really get a memory leak in a C# application (or any GC'd application) is if you create objects and keep references to them (eg. in a list)

    I don't think you're chasing the correct problem here. There isn't even a reason to suspect a memory leak. You should really, really hook your phone up with the profiler to see what's going on.
     
  13. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    hhmmm.. I did use my android with profiler and am not seeing anything out of the ordinary. Since I've set the static scripts to null on destroy, I dont seem to have that drop... :/ Im actually a little concerned now, but also slightly relieved.... If what I did doesn't actually fix it, then where did the glitch go?? Aaarrgghhh..... X|