没有合适的资源?快使用搜索试试~ 我知道了~
Java Garbage Collection Study java 垃圾回收学习
5星 · 超过95%的资源 需积分: 9 12 下载量 152 浏览量
2010-08-21
11:02:24
上传
评论
收藏 4.22MB PDF 举报
温馨提示
试读
28页
Java Garbage Collection Study java 垃圾回收学习
资源推荐
资源详情
资源评论
Java Garbage Collection Study
Mark Volkmann and Brian Gilstrap
Object Computing, Inc.
July 2008
Java GC
•
Java objects are eligible for garbage collection (GC),
which frees their memory
and possibly associated resources,
when they are no longer reachable
•
Two stages of GC for an object
•
finalization - runs finalize method on the object
•
reclamation - reclaims memory used by the object
•
In Java 5 & 6 there are four GC algorithms
from which to choose
•
but one of those won’t be supported in the future,
so we’ll just consider the three that will live on
•
serial, throughput and concurrent low pause
2
GC Process
•
Basic steps
•
object is determined to be unreachable
•
if object has a finalize method
•
object is added to a finalization queue
•
at some point it’s finalize method is invoked
so the object can free associated resources
•
object memory is reclaimed
•
Issues with finalize methods
•
makes every GC pass do more work
•
if a finalize method runs for a long time, it can
delay execution of finalize methods of other objects
•
may create new strong references to objects that had none,
preventing their GC
•
run in a nondeterministic order
•
no guarantee they will be called; app. may exit first
3
The JVM has a
finalizer thread
that is
used for running finalize methods.
Long running
finalize
methods
do not prevent a GC pass from completing
and do not freeze the application.
These are
good reasons
to avoid using
finalize
methods in
safety-critical
code.
Kinds of Object References
•
Strong references
•
the normal type
•
Other reference types in java.lang.ref package
•
SoftReference
•
GC’ed any time after there are no strong references to the referent,
but is typically retained until memory is low
•
can be used to implement caches of objects that can be recreated if needed
•
WeakReference
•
GC’ed any time after there are no strong or soft references to the referent
•
often used for “canonical mappings” where each object
has a unique identifier (one-to-one), and in collections of “listeners”
•
PhantomReference
•
GC’ed any time after there are no strong, soft or weak references to the referent
•
typically used in conjunction with a ReferenceQueue
to manage cleanup of native resources associated with the object
without using finalize methods (more on this later)
4
some
object
soft, weak or
phantom object
target or
“
referent
” object
Also see
java.util.WeakHashMap
.
For soft and weak references, the
get
method
returns
null
when the referent object has been GC’ed.
Alternative to Finalization
•
Don’t write finalize method
in a class whose objects have associated
native resources that require cleanup
•
call this class A
•
Instead, do the following for each such class A
•
create a new class, B, that extends one of the reference types
•
WeakReference, SoftReference or PhantomReference
•
create one or more ReferenceQueue objects
•
a B constructor that takes an A
and passes that, along with a ReferenceQueue object,
to the superclass constructor
•
create a B object for each A object
•
iteratively call remove on the ReferenceQueue
•
free resources associated with returned B object
•
often this is done in a separate thread
5
When there is an associated
ReferenceQueue
,
weak and soft reference are added to it
before
the
referent object has been finalized and reclaimed.
Phantom references are
added
to it after these occur.
剩余27页未读,继续阅读
资源评论
- mslrtc32014-05-12CG虽然不需要自己写但是还是需要注意的啊
- daoba200610302014-07-27马马虎虎,很多地方都有这个同样资料,内容对GC的描述很high level,一些概念没有深入去分析,不是特别好,一般看看,对GC只能起到个大概了解的作用
kzjay
- 粉丝: 6
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- STM32单片机FPGA毕设电路原理论文报告一种基于st62单片机的称重显示控制器
- STM32单片机FPGA毕设电路原理论文报告一种基于SPCE061A单片机的信号发生器
- Ruby菜鸟入门指南.md
- STM32单片机FPGA毕设电路原理论文报告一种基于PIC系列单片机的SPWM逆变电源
- exp01A.cpp
- Rust语言学习万字指南!.md
- STM32单片机FPGA毕设电路原理论文报告一种基于msp430单片机的心电模块设计
- STM32单片机FPGA毕设电路原理论文报告一种基于MSP430单片机的日程管理系统
- web-screen-capture.jar
- 2024最新计算机二级真题练习题库含答案.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功