码迷,mamicode.com
首页 > 编程语言 > 详细

java对象的生命周期

时间:2014-11-15 14:13:56      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   color   ar   使用   sp   java   

原文地址:http://leihuang.net/2014/11/13/Object-Lifecycle/


什么是对象

  • 对象是数据和行为的集合
  • 对象是类的一个实例
  • 对象有状态和行为

创建对象

使用new关键字创建对象是java中最常见的一种方法。

对象生命周期

  1. Created
  2. In use
  3. Invisible
  4. Unreachable
  5. Collected
  6. Finalized
  7. De-allocated

如下图 bubuko.com,布布扣

Created

The following are the some actions performed when an object is created,New memory is allocated for an object.

Once the object has been created, assuming that it is assigned to some variable and then it directly moves to the In Use state.

In use

Objects that are held by at least one strong reference are considered to be “In Use”.

Invisible

An object is in the “Invisible” state when there are no longer any strong references that are accessible to the program, even though there might still be references.

Unreachable

An object enters an “unreachable” state when no more strong references to it exist. When an object is unreachable then it is a state for collection.

It is important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root.

Garbage collection roots are a special class of variable that includes,Temporary variables on the stack

Collected

An object is in the “collected” state when the garbage collector has recognized an object as unreachable and readies it for final processing as a precursor to de-allocation. If the object has a finalize method, then it is marked for finalization.

Finalized

An object is in the “finalized” state if it is still unreachable after it’s finalize method, if any, has been run. A finalized object is awaiting de-allocation. If you are considering using a finalizer to ensure that important resources are freed in a timely manner, you might want to reconsider. To lengthening object lifetimes, finalize methods can increase object size.

De-allocated

The de-allocated state is the final step in garbage collection. If an object is still unreachable after all the above work has done, then this is the state for de-allocation.


2014-11-13 17:53:46

Brave,Happy,Thanksgiving !


java对象的生命周期

标签:des   style   http   io   color   ar   使用   sp   java   

原文地址:http://blog.csdn.net/speedme/article/details/41145131

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!