Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using the profiler with android

Discussion in 'Editor & General Support' started by cephalo2, Jul 17, 2018.

  1. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    I'm having a problem where a piece of my code is causing frames to last about 2 minutes on android. The problem is that the editor, on PC, handles the code fine and doesn't hang like that.

    I'm trying to learn how to use the profiler, and I'm wondering if I can use it to see what function I'm calling that's causing the problem.

    Another difficulty is that the profiler itself is skipping these long frames due to too much data being sent over the USB. I wonder if I can only record the data for my script calls so there is less data being sent.
     
  2. SebastianHjorth

    SebastianHjorth

    Unity Technologies

    Joined:
    Feb 13, 2018
    Posts:
    6
    Hi there! You can use Profiler.logFile to log the profiling capture directly on your android device. You can then transfer the file over and open it in the profiler window to inspect the capture. Be aware that this file can grow quite large quite quickly. Which version of Unity are you using?
     
    cephalo2 likes this.
  3. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    I think I'm using 2018.1. Does the profiling always record the same amount of data, regardless of what columns are disabled?

    EDIT: I am also struggling to find the file I made on my android device. Do I need to use Application.persistentDataPath? If I did use that, where would I access the file? When I set up file transfer through USB, I can't find anything regarding my app, such as the saved game files that I know are on there somewhere.

    EDIT: In my research today, I'm finding that getting files from an android device created by a Unity app is an extremely complex matter. I will definitely need specific instructions on what to call the Profiler.logFile and how to then retrieve the file. I need those missing frames. I just can't find the problem on PC.
     
    Last edited: Jul 18, 2018
  4. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    506
    Try to disable profiler early in the frame with "Profiler.enabled = false;" and enabled it only for a script scope, e.g.
    Code (CSharp):
    1. void Update()
    2. {
    3.     Profiler.enabled = true;
    4.     ...
    5.     Profiler.enabled = false;
    6. }
    That might help with getting the data in the editor for only that fragment.

    You can use Application.temporaryCachePath and add the temporary debug log to figure out full path to the file for your project.
    Do this at the beginning of the frame:
    Code (CSharp):
    1. var profilerDataFilePath = Path.Combine(Application.temporaryCachePath, "myForFrame" + frameN);
    2. Debug.Log("Profiler file for frame " + frameN + ": " + profilerDataFilePath);
    3. Profiler.logFile = profilerDataFilePath;
    4. Profiler.enableBinaryLog = true;
    5.  
    Then with adb pull command you can get files back to desktop and open with profiler.

    Alternatively, you can try to upgrade to 2018.2 and use https://github.com/Over17/UnitySystracePlugin to get markers with systrace tool.
     
    cephalo2 likes this.
  5. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    Thanks so much everyone. I just have a couple more questions. Is there a way to delete these huge files off my phone? Also, can I use the 'Deep Profile' option with this method?

    EDIT: It looks like GC.Alloc has gone ballistic, with 43,668,484 calls, but only accounts for about 10 percent of the frame time. 80 percent of the frame time disappears into my main update loop.
     
    Last edited: Jul 18, 2018
  6. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    506
    There is an experimental way to enable Deep Profiling on Android.
    You need to launch activity with "-e 'unity' '-deepprofiling' " cmdline options.
    Code (CSharp):
    1. adb shell am start -n <player-id>/com.unity3d.player.UnityPlayerActivity "-e 'unity' '-deepprofiling' "
    You might need to use the latest (.Net 4.6) scripting runtime.
    That way you should be able to identify callstack of GC.Alloc.
     
    cephalo2 likes this.
  7. ding0312

    ding0312

    Joined:
    Sep 1, 2015
    Posts:
    3
    I use command line to start Game, but when game start, the ui disappear.