Scripting: Automatic Reference Counting – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "<syntaxhighlight lang="C#">" to "<enforce>") |
Lou Montana (talk | contribs) m (Text replacement - "</syntaxhighlight>" to "</enforce>") |
||
Line 87: | Line 87: | ||
}; | }; | ||
}; | }; | ||
</ | </enforce> | ||
Line 124: | Line 124: | ||
// both objects remain in memory, having 1 reference each (each other) | // both objects remain in memory, having 1 reference each (each other) | ||
}; | }; | ||
</ | </enforce> | ||
==== Solution ==== | ==== Solution ==== | ||
Line 150: | Line 150: | ||
// b loses a's reference and has no more reference to it - it gets released | // b loses a's reference and has no more reference to it - it gets released | ||
}; | }; | ||
</ | </enforce> | ||
Using weak references makes null-checking take more importance in scripting. | Using weak references makes null-checking take more importance in scripting. | ||
{{GameCategory|armaR|Modding|Guidelines|Scripting}} | {{GameCategory|armaR|Modding|Guidelines|Scripting}} |
Revision as of 19:18, 30 July 2022
Automatic Reference Counting, shortened to ARC, is a memory management solution used in Enforce Script. Enforce Script does not use Garbage Collection (a.k.a GC). In comparison with the widely used Tracing garbage collection (in e.g C#, Java):
- it does not suffer from cleanup stuttering (lags due to high GC load)
- memory management performance is not dependent on the number of managed instances
- it is simple to implement (having a good working GC is a big issue)
on the negative side, it is open to the cyclic reference problem, described in the Cyclic Reference paragraph below.
Principle
Enfusion has an internal counter of strong references to objects; when this counter reaches 0, the object is released from memory.
Strong Reference
A strong reference is a reference to an object that increments the reference counter. The referenced object cannot become null during the program's lifetime unless manually deleted (with the delete keyword).
Strong references:
- using the ref keyword
- scope lifetime's script reference
Weak Reference
A weak reference is a reference to an object that does not increment the reference counter. The reference may become null during the program's lifetime.
Usage
Cyclic Reference
Problem
The main issue with reference counting is cyclic references (aka "Island of isolation"). In the code below:
- the main method creates object A and object B
- object A references object B
- object B references object A
- the main method ends, dropping its references to object A and B
- both objects a and b should be deleted but they still reference each other, keeping the reference counting above 0:
- any reference to them is lost after main method's ending
- they remain in memory until the program is stopped
Solution
The solution to this issue is to have one object have a strong reference, while the other only holds a weak one:
Using weak references makes null-checking take more importance in scripting.