Reusing objects is important in Java but also it can cause memory and performance issues if the objects to be reused can't be freed out by GC. Sometimes we need to reuse objects as soon as they are still being referenced or as soon as they may have good chance to be used in the future.
Weak references give us the opportunity to achieve this by not largely affecting memory and performance of our application (In a more GC-friendly). In today's post I will go through the differences between strong and weak references in Java and the effect of them on garbage collector.
Strong References
Strong references are the default type/class references in Java. Objects that are strongly references aren't eligible for GC neither minor nor full GC until they are not strongly referenced anymore.
All object references in Java are strongly referenced unless explicitly specified, see below sections.
StringBuilder builder = new StringBuilder();
Weak References
Weak reference objects are not default and must be explicitly specified. This type of references is to maintain references to objects that are `WEAK` meaning that if the object is eligible for GC `not strongly referenced anymore` but still `weakly referenced` the object can be collected.
StringBuilder builder = new StringBuilder(); WeakReference<StringBuilder> weakBuilder = new WeakReference<>(builder);
Soft References
This type of references is also a weak reference that remains in memory for longer - It resists minor GC until memory is really needed to be reclaimed (Application is reaching OOM then it will clear all soft references). Soft references are essentially one large, least recently used (LRU) object pool (cache).
The JVM keeps track of the last access to each reference and calculates if the soft reference is eligible for GC. This is controlled by the JVM `-XX:SoftRefLRUPolicyMSPerMB` flag which is by default = 1000 = One second of lifetime per free megabyte in the heap
StringBuilder builder = new StringBuilder();SoftReference
<StringBuilder> weakBuilder = newSoftReference
<>(builder);
Phanotm References
Phantom references help us doing finalization to avoid implementing the `Object.finalize` method which could have negative implications to the application as its not deterministic and can make the object reachable again or negatively impact application performance.
Phantom references are different from Weak/Soft references in that GC will not collect a phantom reachable object until its cleared out all acquired resources.
`PhanomReference` class accepts two parameters the referent object and a `ReferenceQueue` which is then used to enqueue objects eligible for clearance.
ReferenceQueue q = new ReferenceQueue(); PhantomReference pr = new PhantomReference(object, referenceQueue); // Later on another point Reference r = q.remove(); // Now, clear up any thing you want
Quick Summary (From: Java Performance - The Definitive Guide)
- Indefinite (soft, weak, phantom) references alter ordinary lifecycle of java objects, allowing them to be reused in ways that may be more GC-friendly.
- Weak references should be used when an application is interested in an object only if that object is strongly referenced else where in the application.
- Soft references hold onto objects for (possibly) long periods of time, providing a simple GC-friendly LRU cache.
- Indefinite reference consume their own memory and hold onto memory of other objects for long periods of time; they should be used sparingly.
Thanks for sharing Very Use ful Blog..
ReplyDeleteDocker Training in Hyderabad
Docker and Kubernetes Online Training
Docker Training
Docker Online Training
Nice Posting !! Thanks for sharing..
ReplyDeleteDocker Training in Hyderabad
Docker and Kubernetes Online Training
Docker Training
Docker Online Training
Kubernetes Online Training
Kubernetes Training in Hyderabad
Best Docker and kubernetes training in ameerpet
Docker and Kubernetes Training in Hyderabad
Good Post
ReplyDeleteYaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
video editing studios in hyderabad
short film editors in hyderabad
corporate video editing studio in hyderabad
ad making company in hyderabad
Nice Information
ReplyDeletePiping Design Course in India
Piping Design Course in Hyderabad
QA / QC Course
QA / QC Course in india
QA / QC Course in Hyderabad
Document Controller course
Pressure Vessel Design Course
Welding Inspector Course
Quality Management Course
Quality Management Course in india
Safety officer course
Thank you for sharing very useful blog!!!!
ReplyDeleteKubernetes Online Training
Docker Online Training
Docker Training in Hyderabad
Kubernetes Training in Hyderabad
انواع العزل المائي
ReplyDeleteالعزل الايجابي: هو منع قطرة الماء من الدخول إلى الجسم المراد عزله وتحويل مسارها إلى مصرف يتم اختياره.
العزل السلبي: تستخدم هذه الطريقة عندما يكون استخدام العزل الايجابي لا يجدي نفعا, حيث يتم عزل المياه من الاتجاه المعاكس لخروج المياه من الجسم .
المعايير الواجب توافرها في المواد العازلة :
مقاومة الماء بكفاءة عالية ومنع تسربات المياه .
ان تتميز بالمرونة العالية حتى ما اذا حدث هبوط بسيط فى المبنى .
خفة الوزن والبساطة فى التركيب .
السماح لبخار الماء من الخروج من المبنى .ذ
جودة المواد اللاصقة ومراعاة الضمير عند التركيب .
مقاومة الظروف والعوامل الجوية المختلفة .
شركة عزل خزانات بالجوف
شركة عزل خزانات بصبيا
شركة عزل خزانات بالرس
شركة عزل خزانات بالدوادمي
Very useful and informative blog. Thank you so much for these kinds of informative blogs.
ReplyDeletewho provides seo services and e-commerce development services.
best website design services in gurgaon
best web design company in gurgaon
best website design in gurgaon
website design services in gurgaon
website design service in gurgaon
best website designing company in gurgaon
website designing services in gurgaon
web design company in gurgaon
best website designing company in india
top website designing company in india
best web design company in gurgaon
best web designing services in gurgaon
best web design services in gurgaon
website designing in gurgaon
website designing company in gurgaon
website design in gurgaon
graphic designing company in gurgaon
website company in gurgaon
website design company in gurgaon
web design services in gurgaon
best website design company in gurgaon
website company in gurgaon
Website design Company in gurgaon
best website designing services in gurgaon
best web design in gurgaon
website designing company in gurgaon
website development company in gurgaon
web development company in gurgaon
website design company
website designing services
I examine your weblog it is excellent and you're doing a super activity for supporting others.
ReplyDeleteI genuinely appreciate what content you are giving to your viewers.
Static Website
PPC Delhi
Software Development Company in India
Software Development Company
Custom Web Development
PHP Web Development Company
OpenCart Development Company
Content Writing Services
Content for Writing
Local Listing On Google
Local Business Google
Web Design and Development
On Page and Off Page Seo
Dynamic Website