@Retention(CLASS) @Target(METHOD) public @interface UnownedLocal
Annotating a single local variable means to exclude instance references of this variable from the reference count. Annotating a method means to withdraw the ownership of all local variables (explicit or implicit) of this method on the referenced object and exclude references of these variables from the referenced object's reference count.
The purpose of UnownedLocal is to reduce the RC overhead, instead of eliminating circular reference.
For the HashMap.containsValue method that is not secure
in multi-threading mode, other threads are not allowed to modify the HashMap synchronously. Therefore, reading of
the internal tables and nodes in the HashMap is not included in the reference count, so that the referenced object
will not be released. In this case, you can use UnownedLocal to annotate the object in the method.
Please refer the following sample code:
@UnownedLocal
public boolean containsValue(Object value) {
Node<K,V>[] tab;
V v;
if ((tab = table) != null && size > 0) {
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
if ((v = e.value) == value || (value != null && value.equals(v)))
return true;
}
}
}
return false;
}
public boolean containsValue(Object value) {
Node<K,V>[] @Unowned tab;
V @Unowned v;
if ((tab = table) != null && size > 0) {
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
if ((v = e.value) == value || (value != null && value.equals(v)))
return true;
}
}
}
return false;
}