Junior — Middle
Millistes olukordades võib viitearvuti kasutamine mälu haldamiseks mitte suurendada objekti arvu?
sobes.tech AI
Vastus AI-lt
Reference counting (reference counting) usage does not increase the counter in the following situations:
- Weak references: they do not increase the counter to avoid cyclic dependencies. For example, in iOS with ARC, weak references are declared with
weak. - Unowned references: similar to weak, but assume the object always exists and also do not increase the counter.
- Internal compiler or runtime optimizations: sometimes, references can be optimized so that the counter does not change.
Example of a weak reference in Swift:
class Person {
var name: String
weak var apartment: Apartment? // does not increase the counter
init(name: String) {
self.name = name
}
}
class Apartment {
var unit: String
init(unit: String) {
self.unit = unit
}
}
Here, apartment is a weak reference that does not increase the counter of the Apartment object.