Java垃圾回收时避免内存碎片的方法
2014-12-05 20:38
218 查看
Compacting Collectors
Compacting collectors slide live objects over free memory space toward one end of the heap. In the process the other end of the heap becomes one large contiguous free area. All references to the moved objects are updated to refer to the new location.
Copying Collectors
A common copying collector algorithm is called "stop and copy." In this scheme, the heap is divided into two regions. Only one of the two regions is used at any time. Objects are allocated from one of the regions until all the space in that region
has been exhausted. At that point program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. When the stop and copy procedure is finished, program execution resumes. Memory
will be
allocated from the new heap region until it too runs out of space. At that point the program will once again be stopped. The heap will be traversed and live objects will be copied back to the original region. The cost associated with this approach
is that twice as much memory is needed for a given amount of heap space because only half of the available memory is used at any time.
Generational Collectors
Generational collectors address this inefficiency by grouping objects by age and garbage collecting younger objects more often than older objects. In this approach, the heap is divided into two or more sub-heaps, each of which serves one "generation"
of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of
the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap.
Compacting collectors slide live objects over free memory space toward one end of the heap. In the process the other end of the heap becomes one large contiguous free area. All references to the moved objects are updated to refer to the new location.
Copying Collectors
A common copying collector algorithm is called "stop and copy." In this scheme, the heap is divided into two regions. Only one of the two regions is used at any time. Objects are allocated from one of the regions until all the space in that region
has been exhausted. At that point program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. When the stop and copy procedure is finished, program execution resumes. Memory
will be
allocated from the new heap region until it too runs out of space. At that point the program will once again be stopped. The heap will be traversed and live objects will be copied back to the original region. The cost associated with this approach
is that twice as much memory is needed for a given amount of heap space because only half of the available memory is used at any time.
Generational Collectors
Generational collectors address this inefficiency by grouping objects by age and garbage collecting younger objects more often than older objects. In this approach, the heap is divided into two or more sub-heaps, each of which serves one "generation"
of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of
the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap.
相关文章推荐
- JVM内存段分配,Java垃圾回收调优,Heap设定,Jvm内存回收算法
- JVM学习笔记(1、 基本结构;2、Java代码编译和执行的整个过程3、内存管理和垃圾回收 4、 内存调优 )
- JAVA垃圾回收机制与内存泄露
- Java的初始化机制、垃圾回收机制和内存分配机制
- java程序设计--孙鑫java无难事Lesson3《包、类和方法说明符、垃圾回收、接口》
- Java垃圾回收机制与内存泄露问题
- Java内存分配与垃圾回收的关系
- JAVA内存的一些总结(三) 垃圾回收
- java 垃圾回收的机制及方法
- Java的初始化机制、垃圾回收机制和内存分配机制
- JAVA垃圾回收机制与内存泄露问题
- Java内存和垃圾回收无废话手册
- java 垃圾回收的机制及方法
- JAVA对内存的使用和垃圾回收
- C++内存回收——浅议C++ 中的垃圾回收方法
- JVM学习笔记(1、 基本结构;2、Java代码编译和执行的整个过程3、内存管理和垃圾回收 4、 内存调优 )
- java 垃圾回收的机制及方法
- JAVA的内存和垃圾回收
- 二.Java的初始化机制、垃圾回收机制和内存分配机制
- 二.Java的初始化机制、垃圾回收机制和内存分配机制