4 ms·
"" Java already has 4 "weak" reference types with different "strengths": SoftReference, WeakReference, Final reference and Phantom "" Is it normal for a langu
by bsaul 11y ago
"" Java already has 4 "weak" reference types with different
"strengths": SoftReference, WeakReference, Final reference and Phantom ""
Is it normal for a language to have that many types of weak references ??
- joewalker 11y agoWeakReferences are elsewhere e.g. [1] My understanding of SoftReferences is that they sound like they're going to be useful in implementing caches, but then cacheing is hard, and generic solutions like a SoftHashMap end up being toys. While they're are probably cases where SoftReferences are useful, they're not common, hence they're not elsewhere. Phantom references are partly due to problems with the way finalize is implemented in Java, and partly due to difficulties writing low level code in a higher level language, hence they're slightly Java specific anyway. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... https://community.oracle.com/blogs/enicholas/2006/05/04/understanding-weak-references https://community.oracle.com/blogs/enicholas/2006/05/04/unde...
- lmm 11y agoSoft/Weak/Phantom are all useful under particular circumstances. All are specialized things that you might only use once or twice in a whole career, but they need language-level support because they're integrated with the GC. Other languages may not have them but that reflects poorly on them if so. It's similar to how Java GC has a lot of tweakable parameters, which seems like a bad thing until you realise that you need those options just as badly in other languages, it's just that you don't have them. Final reference is a wart that probably wouldn't be there if the language were designed today. But again, being source and binary compatible with code from 1995 is one of Java's greatest strengths.