ShoulO
ShoulO

Reputation: 468

clearing memory after arrays are used in unity

I want to clear memory from "spent" arrays of vectors or integers. Arrays themselves and values inside are no longer necessary. I am simply doing:

System.Array.Clear(SomeArray,0,SomeArray.Length );
SomeArray=null;

Does this clear the memory and it becomes available for other uses? Or does GC still have to do it? (if so how to do it without leaving any to GC )

P.S. I figured that iterating through and nulling each value is pointless since you can't null vector or int.

Thanks

Upvotes: 0

Views: 3409

Answers (2)

Pavan Chandaka
Pavan Chandaka

Reputation: 12821

This method only clears the values of the elements; it does not delete the elements themselves.

Look into below documentation: https://msdn.microsoft.com/en-us/library/system.array.clear(v=vs.110).aspx

By setting NULL is not an indication to GC to collect it. Probably you can call GC.Collect(). refer https://msdn.microsoft.com/en-us/library/system.gc.collect(v=vs.110).aspx for more information.

Also keep in mind "GC.Collect" call is a overhead.

Upvotes: 4

Matias Cicero
Matias Cicero

Reputation: 26321

Setting SomeArray to null only tells the GC that the array is no longer referenced by that variable and may be available1 for collection.

Garbage collection can happen at any time.

Until then, the array that was referenced by SomeArray will still occupy the same amount of memory. The only difference being that it would now contain zeroed elements.


1 The array may be still being referenced by other variables. GC will only collect it when it is no longer referenced anywhere in your program.

Upvotes: 2

Related Questions