你的位置:首页 > 软件开发 > Java > 垃圾回收机制和数据结构栈链表

垃圾回收机制和数据结构栈链表

发布时间:2016-02-08 16:00:06
1、垃圾回收机制:(1)没有引用变量指向的对象,就是垃圾。举例: Test t = new Test(); t=null;那么之前创建的对象就是垃圾。(2)对象没有被使用是 ...

垃圾回收机制和数据结构栈链表

1、垃圾回收机制:

(1)没有引用变量指向的对象,就是垃圾。

举例:

        Test t = new Test();

        t=null;

那么之前创建的对象就是垃圾。

(2)对象没有被使用是另外一种垃圾。

    new Test();

    new Test().toString();

区别在于第一个对象很明显没有指向,是垃圾。但是第二个不是,因为他被使用了。

 

2、回收时机。

通常情况下,要在满了的时候回收。

其次在调用

    System.gc();//通常情况下会立刻回收。等效于Runtime.getRuntime.gc();

 

3、finalize() 方法,是任何对象都有的一个方法。(Object )

    任何对象在被回收之前都会调用的一个方法。finalize().

这个方法的函数体是空的。

 

 

方法描述:

Runs the garbage collector.Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The call System.gc() is effectively equivalent to the call:

 Runtime.getRuntime().gc()

运行垃圾回收器:

调用gc方法 建议java虚拟机增强性能通过回收未利用的对象,让内存中已经被占用的快速能够再次被使用。当控制器返回这个方法调用后,java虚拟机能够更好地重新声明控件,从所有废弃的对象那里。

调用System.gc()方法等效于 Runtime.getRuntime.gc();    

 

记得找到 class 来执行

垃圾回收机制和数据结构栈链表

 

Code:package GarbageCollectin;public class GarbageCollection {    public static void main(String[] args) {        GarbageCollection gc = new GarbageCollection();     new GarbageCollection();     new GarbageCollection().toString();     gc = null;     Runtime.getRuntime().gc();//等效于 System.gc();         System.runFinalization();     //这样直接运行并没有结果。  }}

原标题:垃圾回收机制和数据结构栈链表

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录