r/Unity3D•
SwillyDo

How do I make sure an instantiate class has been garbage collected?

Question

Title should say "instantiated".

I have a ScriptableObject A that instantiates a Class B, which does some stuff and then is needed no more. I can't destroy a class instance directly; I have tried removing all references to the class but I don't know how to tell if it's been collected. It's not a Monobehaviour so there is no OnDisable() in which I can put a debug.log.

Any reference to check if it is still alive will presumably prevent garbage collection, so it feels like Heisenberg's Uncertainty Principle.

Do I just wait and see if performance is hit by a memory leak...?

Posted Sep 5, 2019, 9:19 PM UTC

18
[deleted]•

[deleted]

1
Kishotta•

That uncertainty is the reason Garbage Collection exists. Remember that your code is compiled to the CLR. The CLR will make all variable declarations (and memory allocations) on your behalf. Unless you want to play around with unsafe code, memory leaking in C# shouldn't ever happen. You may, however, run into issues when instantiating and destroying large numbers of objects, which would be very taxing on the Garbage Collector.

If you still have concerns, you can try to refactor your code to use structs, which are allocated on the stack, and thus don't generate garbage.

1
SwillyDoOP•

I am just trying to plan basic efficiency. Like, if I accidentally leave a reference to my instantiated classes, they will just keep piling up until they impact performance or a scene-change wipes them, right?

Is there no way to tell when they've been collected or to confirm that all references have been removed?

1
Kishotta•

Unfortunately, no, there isn't a good general purpose way to check for references (although I've heard of some unity specific assets that can track game object scene references).

Proper encapsulation can help with this though. If a class contains all of its own data and functionality (and nothing else), and doesn't expose it to the outside world, you can be confident that when the object goes out of scope or is destroyed, it'll take all of its members with it.

Sorry if I'm not answering the right questions for you.

2
SwillyDoOP•

No no, you're on the right topic exactly! I need another script to be told when this script has finished its business so there a delegate and subscription involved.

Here is what I have done to remove references:

- Unsubscribed from the delegate.

- Remove the script from a list.

- Set a direct reference to null.

I'm pretty sure that's everything, I was just hoping there was a more concrete way to make sure it was done properly. Thanks!

2
Kishotta•

If you're needing to replicate that setup in several places, you might look into something like the command pattern? I think you're in ok shape though. Good luck!

2
MaxwelldoggumsProfessional•

Nope.

The purpose of the garbage collector is to prevent instances from piling up and impacting performance. Unreferenced instanced are guaranteed to be cleaned up, but you can’t feasibly know when. The C# runtime will decide when that happens. Usually it’s when the amount of garbage reaches a certain threshold, or if the programmer manually requests a collection.

Coming from C or C++, it’s frustrating to not know that allocations have been correctly freed, but the underlying philosophy of C# is that you shouldn’t ever need to.

--

As an addendum, you can manually allocate and free memory. This is useful for extremely performance critical code. I'm working on a voxel terrain system, and the data streaming service has explicit control over allocated memory. That being said the language goes to great lengths to discourage this unless you really need it.

1
SwillyDoOP•

Yeah I don't want to get into manually managing memory - 100% happy for that process to remain automated. I was just hoping there was a way to be sure an class instance was flagged for collection, regardless of when the collection actually takes place.

1
MaxwelldoggumsProfessional•

There really isn’t a good way to do this.

The garbage collector itself decides when an instance is no longer needed. You can trigger the garbage collection process manually with ‘GC.Collect()’, but even then there’s no guarantee that it will actually happen.

If your application is absolutely performance critical (processing tons of data, etc.) the best bet is to avoid the creation of garbage altogether.

1