Garbage collection in Java

Automatic Garbage Collection: is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. 

The basic process are as follows:

Step 1: Marking. This is the garbage collector identifies which pieces of memory are in use and which are not.

Step 2: Normal Deletion: Normal deletion removes the unreferenced objects leaving referenced objects and pointers to free space.

Step 2a: In addition to deleting unreferenced objects, we can compact the remaining referenced objects. By moving refernced object together, this makes new memory allocation much easier and faster.

The head is broken up into smaller parts or generations. The heap parts are: Young Generation, Old or Tenured Ganeration, and permanent Gneration

Young Generation: is where all new objects are allocated and aged. (Minor garbage collection)

Old Generation: store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. (Major garbage collection).

Permanent generation: contains metadata required by the JVM to describe the classes and methods used in the application. 


The Generational Garbage Collection Process

1. First, any new objects are allocated to the eden space. Both survivor spaces starts out empty.

2. When the eden space fills up, a minor garbage collection is triggered.

3. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.

4. At the next minor GC, the same thing happeneds for the eden space. In this case, they are moved to the second survivor space.

5. At the next minor GC, the same process repeats. survivor space switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.

6. Promotion. when aged objects reach a certain age threshold, they are promoted from young generation to old generation.

7. continue promoted to the old generation space.

8. after cover the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.

The more detailed explain is over there: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html


Invoke Java Garbage Collection

System.gc() method which simply a hint to the garbage collector that you want it to do a collection.

System.runFinalization() method to tell do any pending finalizers to be run for all the eligible objects for GC. 


long freeMemory(): return an amount of free memory in bytes available for new objects use. 

long totalMemory() to find total amount of memory in bytes available in JVM.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Thinking in Java》是由Bruce Eckel撰写的一本Java编程的经典教材,该书以英文语言出版。以下是该书的目录大纲: Chapter 1: Introduction to Objects - Introduction - The progress of abstraction - How objects communicate - Every object has an interface - The process of object-oriented design - Implementation hiding - Inheritance and reuse - Polymorphism and dynamic binding - Summary of object-oriented principles Chapter 2: Everything Is an Object - Primitive types - How the objects are created - Aliasing: All arguments are passed by value - Documentation comments - Controlling access to members of a class - First exposure to Java syntax Chapter 3: Operators - Thinking recursively - Operator precedence - Assignment with objects - Mathematical operators and precedence - Autoboxing and unboxing - Increment and decrement - Java logic operators - Bitwise operators - The instanceof operator - Summary of operators Chapter 4: Control Structures: Part 1 - True and false - The if-else statement - The switch statement - The while statement - The do-while statement - The for statement - Summary of control structures Chapter 5: Control Structures: Part 2 - The break and continue keywords - Foreach and multidimensional arrays - Using the comma operator - The return keyword - Summary of control structures Chapter 6: Initialization & Cleanup - Member initialization - Constructor initialization - Method overloading & generic methods - Order of initialization - Constructor & parameter lists - Garbage collection - The finalize() method - Summary of initialization and cleanup ......(接下去的章节继续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值