Java Performance Tuning - Weak Reference

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值