没有合适的资源?快使用搜索试试~ 我知道了~
Plumbr Handbook Java Garbage Collection
需积分: 25 12 下载量 193 浏览量
2018-05-04
14:55:00
上传
评论
收藏 2.61MB PDF 举报
温馨提示
试读
75页
jvm日志说明文档。很详细。可以作为查找类书籍。当遇到看不懂的日志。可以在该文件上找
资源推荐
资源详情
资源评论
Java Garbage Collection
Explanations
Code examples
Practical advice
Plumbr Java Garbage Collection Handbook 2015 2
Authors
Nikita Salnikov-Tarnovski is a co-founder of Plumbr, the JVM
performance monitoring product, where he now contributes as VP of
Engineering. Besides his daily technical tasks of solving various problems
and helping the team move forward, he is an active blogger, JavaOne
RockStar and a frequent conference speaker (JavaOne, Devoxx,
GeeCON, TopConf, JavaDay, GeekOut, Joker, JPoint, Jazoon etc). Find
him on Twitter: @iNikem.
Gleb Smirnov is leading the JVM internals monitoring team at Plumbr. He
enjoys sharing knowledge and helping others find their way through leaky
abstractions. As a result, you can often see him speaking at conferences
(JavaOne, Devoxx, GeeCON, JEEConf, GeekOut, Joker, JCrete, and
others), blogging (plumbr.eu/blog, gvsmirnov.ru/blog/), and tweeting
(@gvsmirnov).
Table of Contents
What Is Garbage Collection?............................................................................................................. 3
Garbage Collection in Java ............................................................................................................... 7
GC Algorithms: Basics .................................................................................................................... 15
GC Algorithms: Implementations ..................................................................................................... 18
GC Tuning: Basics .......................................................................................................................... 39
GC Tuning: Tooling ......................................................................................................................... 47
GC Tuning: In Practice .................................................................................................................... 62
Plumbr Java Garbage Collection Handbook 2015 3
What Is Garbage Collection?
At first sight, garbage collection should be dealing with what the name suggests – finding and throwing away
the garbage. In reality it is doing exactly the opposite. Garbage Collection is tracking down all the objects that
are still used, and marks the rest as garbage. Bearing this in mind, we start digging into more details of how the
process of automated memory reclamation called ‘Garbage Collection’ is implemented for Java Virtual Machine.
Instead of rushing into specifics, we shall start from the very beginning, explaining the general nature of garbage
collection and the core concepts and approaches.
Disclaimer: This handbook focuses on Oracle Hotspot and OpenJDK behavior. In other runtimes or even on
other JVMs, such as jRockit or IBM J9, some of the aspects covered in this handbook can behave differently.
Manual Memory Management
Before we can start covering Garbage Collection in its modern form, let’s do a quick recap of days where you
had to manually and explicitly allocate and free memory for your data. And if you ever forgot to free it, you would
not be able to reuse the memory. The memory would be claimed but not used. Such a scenario is called
a memory leak.
Here is a simple example written in C using manual memory management:
int send_request() {
size_t n = read_size();
int *elements = malloc(n * sizeof(int));
if(read_elements(n, elements) < n) {
// elements not freed!
return -1;
}
// …
free(elements)
return 0;
}
As we can see, it is fairly easy to forget to free memory. Memory leaks used to be a lot more common problem
than today. You could only really fight them by fixing your code. Thus, a much better approach would be to
automate the reclamation of unused memory, eliminating the possibility of human error altogether. Such
automation is called Garbage Collection (or GC for short).
Smart Pointers
One of the first ways to automate garbage collection was built upon reference counting. For each object, you
simply know how many times it is referred to and when that count reaches zero the object can be safely
reclaimed. A well-known example of that would be the shared pointers of C++:
Plumbr Java Garbage Collection Handbook 2015 4
int send_request() {
size_t n = read_size();
shared_ptr<vector<int>> elements
= make_shared<vector<int>>();
if(read_elements(n, elements) < n) {
return -1;
}
return 0;
}
The shared_ptr that we are making use of keeps track of the number of references to it. This number increases
as you pass it around and decreases as it leaves scope. As soon as the number of references reaches zero,
the shared_ptr automatically deletes the underlying vector. Admittedly, this example is not exactly prevalent in
the real world, but it is enough for demonstrational purposes.
Automated Memory Management
In the C++ code above, we still had to explicitly say when we want to have memory management to be taken
care of. But what if we could make all the objects behave this way? That would be very handy, since the
developers no longer have to think about cleaning up after themselves. The runtime will automatically
understand that some memory is no longer used and frees it. In other words, it automatically collects the
garbage. The first garbage collector was created in 1959 for Lisp and the technology has only advanced since
then.
Reference Counting
The idea that we have demonstrated with the shared pointers of C++ can be applied to all objects. Many
languages such as Perl, Python or PHP take this approach. This is best illustrated with a picture:
The green clouds indicate that the object that they point to is still in use by the programmer. Technically, these
may be things like a local variable in the currently executing method or a static variable or something else. It
may vary from programming language to programming language so we will not focus on it here.
Plumbr Java Garbage Collection Handbook 2015 5
The blue circles are the live objects in memory, with the numbers inside denoting their reference counts. Finally,
the grey circles are objects that are not referenced from any object that is still explicitly in use (these are directly
referenced to by the green clouds). The grey objects are thus garbage and could be cleaned by the Garbage
Collector.
This all looks really good, does it not? Well, it does, but the whole method has a huge drawback. It is quite easy
to end up with a detached cycle of objects none of which are in scope yet due to cyclic references the count of
their reference is not zero. Here’s an illustration:
See? The red objects are in fact garbage that the application does not use. But due to the limitations of reference
counting there is still a memory leak.
There are some ways to overcome this, such as using special ‘weak’ references or applying a separate algorithm
for collecting cycles. The aforementioned languages – Perl, Python and PHP – all handle cycles in one way or
another, but this is outside the scope of this handbook. Instead, we will start investigating the approach taken
by the JVM in more details.
Mark and Sweep
First of all, the JVM is more specific about what constitutes reachability of an object. Instead of the vaguely
defined green clouds that we saw on earlier chapters, we have a very specific and explicit set of objects that are
called the Garbage Collection Roots:
Local variables
Active threads
Static fields
JNI references
The method used by JVM to track down all the reachable (live) objects and to make sure the memory claimed
by non-reachable objects can be reused is called the Mark and Sweep algorithm. It consists of two steps:
Marking is walking through all reachable objects starting from GC roots and keeping a ledger in native
memory about all such objects
Sweeping is making sure the memory addresses occupied by non-reachable objects can be reused by the
next allocations.
剩余74页未读,继续阅读
资源评论
qq_38635013
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【创新创业材料】某服装进入XX市市场可行性报告 .zip
- 【创新创业材料】某市图书馆建设项目可行性研究报告.zip
- 【创新创业材料】某医疗器械股份有限公司核心产品国内外营销网络建设项目可行性研究报告-优秀甲级资质可研报告.zip
- 【创新创业材料】某化工公司氨基酸原料及磷化工产品生产项目可行性研究报告-极品推荐140页优秀甲级资质可研报告.zip
- 质量运营与模式i建构分享 - 转PDF.pdf
- JAVA中Spring框架入门与实践心得.zip
- 【创新创业材料】泸州老窖中长期发展战略.zip
- 【创新创业材料】旅游地产:山东胶南小珠山旅游&文化创意地产项目可行性研究报告2008-138页.zip
- main.c
- Unity 创建快捷方式自动启动
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功