欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Performance Tuning - Weak Reference performanceJavaIBMSUNGo 

程序员文章站 2022-07-12 11:10:50
...

In the best practise of java performance tuning
We always heard about that: The long term object hold a short term object is one of the most likely way to lead to performace issue.

What is that supposed to mean? It absolutely doesn't make any sence, So let's get it more concrete.

Let's say In a service class. There is a global Map,  which hold the user's order entry. The Entry is a transient object which reserve only for a short time. But when the global map fill the Entry, It's exist as long as the global Map. With time, the global map become bulky coz some many short term object has not been collected which are meant to collected!! 

So in this case, the weak reference come to rescue.

The Entry set in the global Map is a weak reference. If Entry Object is only reference by the weak reference ,then the entry will be the candidate for gc.

For details on

http://www-128.ibm.com/developerworks/java/library/j-jtp11225/     Give a brief introduction with Weak reference.

http://java.sun.com/developer/technicalArticles/ALT/RefObj/              Official iintroduction to Reference Object from Sun.

What is soft reference?
If the only remaining references to an object are weak or soft references, then that object is said to be softly reachable. The garbage collector does not aggressively collect softly reachable objects the way it does with weakly reachable ones -- instead it only collects softly reachable objects if it really "needs" the memory. Soft references are a way of saying to the garbage collector, "As long as memory isn't too tight, I'd like to keep this object around. But if memory gets really tight, go ahead and collect it and I'll deal with that." The garbage collector is required to clear all soft references before it can throw OutOfMemoryError.

More details about soft reference. Please refer
http://www.ibm.com/developerworks/java/library/j-jtp01246.html?S_TACT=105AGX02&S_CMP=EDU