Java知识分享网 - 轻松学习从此开始!    

Java知识分享网

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus权限系统实战课程 震撼发布        

最新Java全栈就业实战课程(免费)

springcloud分布式电商秒杀实战课程

IDEA永久激活

66套java实战课程无套路领取

锋哥开始收Java学员啦!

Python学习路线图

锋哥开始收Java学员啦!
当前位置: 主页 > Java文档 > Java基础相关 >

Plumbr Handbook Java Garbage Collection PDF 下载


分享到:
时间:2020-10-11 07:55来源:http://www.java1234.com 作者:转载  侵权举报
Plumbr Handbook Java Garbage Collection PDF 下载
失效链接处理
Plumbr Handbook Java Garbage Collection PDF 下载

本站整理下载:
提取码:1am3 
 
 
相关截图:
 
主要内容:

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:

 

------分隔线----------------------------

锋哥公众号


锋哥微信


关注公众号
【Java资料站】
回复 666
获取 
66套java
从菜鸡到大神
项目实战课程

锋哥推荐