Please enable JavaScript.
Coggle requires JavaScript to display documents.
Essential Java (Creating/Destroying Objects p28 (eliminate obsolete object…
Essential Java
Creating/Destroying Objects p28
-
-
pass factory of dependent resource to the constructor, Supplier<T> is perfect for factory
-
-
eliminate obsolete object references p60
set null to reference no longer needed (mostly by letting the variable containing the reference fall out of scope)
:!:Whenever a class manages its own memory, beware of memory leak. Set null to objects no longer needed in the self-managed memory
use weak reference in Cache (e.g. LinkedHashMap) to avoid memory leak (:!: work ONLY when lifetime is determined by the key, NOT value)
Listener / Callback can memory leak if not deregistered explicitly. Store them only as keys in WeakHashMap
Avoid Finalizers and Cleaners p64
unpredictable behaviour, large performance impact, just DON'T use them
:!: serious security problem - when exception is thrown from constructor or equivalent serialization readObject/Resolve,
finalizer of a malicious subclass can run on the partially constructed object which should've died.
Solution - write a no-op final finalizer method for non-final classes
prefer try(resource) over try-finally p72
-
Methods Common to all Objects p76