- Home /
Optimize (Garbage Collection, Memory usage)
I have some questions about Optimization.
I've heard that garbage collection will be executed automatically when developing for Windows in Unity. Is there any way to manually collect garbage in order to not affect game performance ?
Second, is there any performance or memory usage differences between those 2 code scripts ?
Code Script 1:
#pragma strict
private static var constNumber : float = 0.5f ;
function Update() { var fltCount: float = 0.7 + time.deltaTime fltCount -= constNumber }
Code Script 2
#pragma strict
private static var constNumber : float = 0.5f ; private var fltcount: float ;
function Update() { fltCount = 0.7 + time.deltaTime fltCount -= constNumber //could be written in one line but that's not my essential question }
Third question: When using the yield instructions, will there be a other CPU core used ? (If available)
Answer by Statement · Feb 16, 2011 at 11:34 AM
1. You can probably use GC.Collect to manually tell garbage collector to clean out old data, although I haven't done manually so with Unity/Mono.
You also might want to look for pointers about when to use it.
2. Negligible difference, if any at all. I wouldn't worry. You can probably remove one arithmetic operation if you do:
fltCount = (0.7f - constNumber) + time.deltaTime;
Because the first two values on right side are constant and can be computed compile time (Parenthesis added for clarity).
The compiler should output:
fltCount = 0.2f + time.deltaTime;
EDIT: Actually, your constNumber is a static, and the compiler won't be able to make this assumption!
3. Yield instructions don't run threaded, so no. You must create your own threads for concurrent/parallel execution. When using threads to utilize several cores you must take action to avoid state corruption, deadlocks, and not call most of the Unity functions as they are generally not thread safe.
Thank you for your help. So declaring the variables outside the function ins$$anonymous$$d of inside the function doesn't affect performance ?
If you are curious about it, you can easily benchmark this yourself (and is a great way of understanding performance!). You can do a for loop that does this operation 100k times or something like that, for both scenarios and compare the results. Note that having a member variable adds to your complexity (ease of use) and increases your memory consumption since it has to tag along another float. I wouldn't worry, and I wouldn't use the member variable for this task. The time spent in your operation is magnitudes lesser than just the process of calling Update :)
Sometimes, the loop itself take up most of the computational time, so one way to help this would be to unroll the loop a bit, so you repeat the code several times within the loop. For the temporary variable, you can embed it inside a scope to avoid having to redefine the variable with different names, using { code; code; }. This would probably produce a fairer result.
Actually, your constNumber isn't "const". The variable name fooled me :) It's "static", which doesn't hold true that the compiler will generate in-place computation. In either case, this is such a trivial example and your code will run just fine either case.