Search Unity

What is Managed Global type?

Discussion in 'Profiler Previews' started by Kichang-Kim, Feb 10, 2020.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    720
    Hi. I found that my assets are not released due to "Managed Global" type reference.

    It has no Value, NativeObjectName, RefCount and other fields so I can't track anymore.

    What is this and how to find it?

    Thanks.

    (Memory Profiler 0.2.0-p1)
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,549
    Please update to com.unity.memoryprofiler@0.2.1-preview.2 and open the snapshot again. This should now show the Native object behind that Managed Object wrapper.

    Please ignore Managed Global for now.

    Managed Global is a virtual construct in the Memory Profiler UI and an incorrect stand-in for the Mono Manager, which would hold a reference on a MonoBevahiour inherited type instance until it is destroyed. Destruction would kill the native object behind the managed one and unhook the managed object from the Mono Manager, at which point the Managed object will be collected during the next GC swipe. So if with this new version you still see the Managed Object without a reference from the native one, it might be a failure to crawl the snapshot correctly or the object is about to be collected.
     
    Kichang-Kim likes this.
  3. AGulev-k

    AGulev-k

    Joined:
    Jan 16, 2020
    Posts:
    8
    @MartinTilo hey, I still see Managed Global data type in
    "com.unity.memoryprofiler": "0.2.6-preview.1"
    upload_2020-11-11_17-52-19.png
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,549
    Yeah, we still need to get rid of it. I think some obstruse code might've still relied upon it existing?
     
  5. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    84
    @MartinTilo I still see this Managed Global in 0.2.9-preview.3. I'm trying to chase down what references a managed type which isn't being garbage collected. The profiler says this managed type has 1 reference, and when I click on that it takes me to this Managed Global <global> line as shown in the screenshot a few posts ago. This kinda leaves me in a dead end. Any ideas when this will be removed? Is there another way to proceed?
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,549
    Hi @hugeandy
    We have indeed not removed the Managed Global reference in the package yet. I've worked a bit on this today and it'll very likely be gone in the next update (0.4.0).

    That said and while looking over this, what I found was that Managed Global should really just be ignored. If it is the only reference you have on an object, and that Object isn't a 0-length array of some Unity type (which might be down to an attribute we use to hold these in memory as a way to prevent the types from being compiled out while needed from our native code) then it should be garbage collected.

    If it isn't, you might've hit a bug in the reference tracking between Managed and Native UnityEngine.Object inheriting types. Please make sure your Unity version is updated to 2021.2.0a12, 2021.1.9, 2020.3.12f1, 2019.4.29f1 or newer.

    If you are on these or a later one, are sure the object didn't get created since the last GC sweep and had 0 ref count before (you can check that it is the same object by diffing a snapshot from before and after triggering GC) then please file a bug report. There is a chance that the Managed and Native Unity Object tracking still has a bug that we somehow missed, or that some other magic / attribute holds on to this object and is somehow missed in the Managed Snapshot (like the [UsedByNativeCode], [RequiredByNativeCode] attributes).
     
    Last edited: Jul 22, 2021
  7. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    84
    Hey @MartinTilo

    I discovered that the objects that had 1 reference were managed objects that I had created GCHandles for with GCHandle.Alloc in order to pass them to the job system. I had not called Free() on the handles when I was done, so that was why the objects weren't getting collected. So, the profiler was indeed correct to say that there was 1 reference to objects, but it showed it as the Managed Global type. Is that expected?

    Cheers,
    Andy
     
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,549
    Oh... right. Yeah, tracking down objects where you explicitly created GCHandles for is not something the memory profiler can do yer. So that would just show 0 until we maybe find out some magic we can do there... So no, showing 1 reference for that is not expected, 0 would be expected in that case.

    Did you use a Pinned GCHandle? For those we may eventually have something more explicit so we can list these objects as pinned (that needs a change in the main Unity codebase though). If we can't find a corresponding GCHandle field anywhere in the capture, we can maybe even list these out as potentially leaked, but not really if they are pinned via UnsafeUtility unless we want to do some guesswork on all ulong fields in the capture...
     
  9. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    84
    Didn't use a pinned GCHandl

    Our problem is solved now, and hopefully someone might have the same problem and find this post helpful

    Cheers for the help! The memory profiler is great also so good work on that! Looking forward to whatever improvements are coming up
     
    MartinTilo likes this.
  10. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,549
    Update: it's gonna be definitely gone in the 0.4.0 update but not in the next one, as we had to throw in a patch release before 0.4.0 was ready, so 0.2.10-preview.1 still has this in it.
     
    hugeandy likes this.
unityunity