Junior — Middle
Can you list the main types of references in heap memory?
sobes.tech AI
Answer from AI
In the heap memory area in Java (and Android), there are the following main types of references that affect garbage collection:
-
Strong Reference: a normal reference to an object. As long as a strong reference exists, the object will not be garbage collected.
-
Soft Reference: used for caching. Objects with soft references are only removed when memory is low.
-
Weak Reference: objects with such references are removed during the next garbage collection pass, even if weak references still exist.
-
Phantom Reference: used to track object removal from memory, does not allow direct access to the object.
Example of creating a weak reference:
WeakReference<MyObject> weakRef = new WeakReference<>(myObject);