java.lang.ref.cleaner包

finalize()//该方法已过时,有风险,慎用

1.对象不可能被垃圾回收

2.垃圾回收并不等于"析构"

只有当垃圾回收发生时finalize()才得到调用,否则对象就会一直保存下来,知道程序结束,将资源交还系统

3.垃圾回收只与内存又关,垃圾回收和终结都不保证会一定发生  //可以使用system.gc() 提醒 虚拟机回收

System.runFinalization(); //强制调用已经失去引用的对象的finalize方法

可以使对象引用指向null强制对象失去引用

//: object/TerminationCondition.java
/**@version 1.0
* @author fei long
*/
package object; class Book {
boolean checkedOut = false;
Book(boolean checkOut)
{
checkedOut = checkOut;
}
void checkIn()
{
checkedOut =false;
}
protected void finalize()
{
if(checkedOut)
{
System.out.println("Error: checked out");
}
}
}
public class TerminationCondition{
public static void main(String[] args)
{
Book novel = new Book(true);
novel.checkIn();
new Book(true);
System.gc(); //提醒虚拟机回收
System.runFinalization();//强制调用已经失去引用的对象的finalize方法 
}
}/* output
Error: checked out
*///~

继承和清理: 通过组合和继承方法来创建新类时,永远不必担心对象的清理为你,子类通常留给垃圾回收器进行处理,如果遇到清理的问题,那么必须为新类创建dispose()方法,并且由于继承的缘故,如果我们有其它作为垃圾回收一部分的特殊清理动作,就必须在导出类中覆盖dispose方法,当覆盖被继承类的dispose()方法时,务必调用基类版本的dispose()方法,否则基类的清理动作就不会发生,

//: polymorphism/Forg.java
// cleanup an inheritance
package object; import static net.mindview.util.Print.*; class Characteristic{
private String s;
Characteristic(String s)
{
this.s = s;
print("Creating Characteristic " + s);
}
protected void dispose()
{
print("disposing characteristic " + s);
}
}
class Description{
private String s;
Description(String s)
{
this.s = s;
print("Creating Description " + s);
}
protected void dispose()
{
print("disposing description" + s);
}
}
class LivingCreature{
private Characteristic p =
new Characteristic("is alive");
private Description t =
new Description("Basic living creature");
LivingCreature()
{
print("livingcreature");
}
protected void dispose()
{
print("livingCreature dispose");
t.dispose();
p.dispose();
}
}
class Animal extends LivingCreature{
private Characteristic p =
new Characteristic("has heart");
private Description t =
new Description("animal not vegetalbe");
Animal(){print("Animal()");}
protected void dispose()
{
print("Animal dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
class Amphibian extends Animal
{
private Characteristic p =
new Characteristic("can live in water");
private Description t =
new Description("Both water an land");
Amphibian()
{
print("Amphibian()");
}
protected void dispose()
{
print("Amphibian dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
public class Frog extends Amphibian{
private Characteristic p = new Characteristic("Croaks");
private Description t = new Description("Eats bugs");
public Frog()
{
print("Forg()");
t.dispose();
p.dispose();
super.dispose();
}
public static void main(String[] args)
{
Frog frog = new Frog();
print("bye!");
frog.dispose();
}
}/* output: 消费的顺序和初始化的顺序相反
Creating Characteristic is alive
Creating Description Basic living creature
livingcreature
Creating Characteristic has heart
Creating Description animal not vegetalbe
Animal()
Creating Characteristic can live in water
Creating Description Both water an land
Amphibian()
Creating Characteristic Croaks
Creating Description Eats bugs
Forg()
disposing descriptionEats bugs
disposing characteristic Croaks
Amphibian dispose
disposing descriptionBoth water an land
disposing characteristic can live in water
Animal dispose
disposing descriptionanimal not vegetalbe
disposing characteristic has heart
livingCreature dispose
disposing descriptionBasic living creature
disposing characteristic is alive
bye!
Amphibian dispose
disposing descriptionBoth water an land
disposing characteristic can live in water
Animal dispose
disposing descriptionanimal not vegetalbe
disposing characteristic has heart
livingCreature dispose
disposing descriptionBasic living creature
disposing characteristic is alive
*///~

三,当一个成员对象存在于其它一个或多个对象共享的情况时,也许就必须使用引用计数来跟踪仍旧访问着共享对象的对象数量了.

//: polymorphism/ReferenceCounting.java
// Cleaning up shared member objects
package object;
import static net.mindview.util.Print.*; class Shared{
private int refcount =0;
private static long counter = 0; //记录该类总共的对象个数
private final long id = counter++; //final 表示不希望再对象生命周期内ID的值被改变
public Shared()
{
print("Creating " + this);
}
public void addRef(){refcount++;} //记录同一个对象被引用的次数
public void dispose()
{
if(--refcount == 0) //当被引用数只有一个时执行清理
print("Disposing " + this);
}
public String toString(){return "Share " + id;}
}
class Composting{
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composting(Shared shared)
{
print("Creating " + this);
this.shared= shared;
this.shared .addRef();
}
protected void dispose()
{
print("disposing " + this);
shared.dispose();
}
public String toString(){return "Composing " + id;}
}
public class ReferenceCounting{
public static void main(String[] args)
{
Shared shared = new Shared();
Composting[] composting = { new Composting(shared),
new Composting(shared),new Composting(shared),
new Composting(shared),new Composting(shared),
};
for(Composting c : composting)
c.dispose();
}
}/* output:
Creating Share 0
Creating Composing 0
Creating Composing 1
Creating Composing 2
Creating Composing 3
Creating Composing 4
disposing Composing 0
disposing Composing 1
disposing Composing 2
disposing Composing 3
disposing Composing 4
Disposing Share 0
*///:~

Java 清理和垃圾回收的更多相关文章

  1. Java编程思想学习笔记_1(Java内存和垃圾回收)

    1.Java中对象的存储数据的地方: 共有五个不同的地方可以存储数据. 1)寄存器.最快,因为位于处理器的内部,寄存器按需求分配,不能直接控制. 2)堆栈.位于通用RAM,通过堆栈指针可以从处理器那里 ...

  2. Java虚拟机之垃圾回收详解一

    Java虚拟机之垃圾回收详解一 Java技术和JVM(Java虚拟机) 一.Java技术概述: Java是一门编程语言,是一种计算平台,是SUN公司于1995年首次发布.它是Java程序的技术基础,这 ...

  3. 【java虚拟机序列】java中的垃圾回收与内存分配策略

    在[java虚拟机系列]java虚拟机系列之JVM总述中我们已经详细讲解过java中的内存模型,了解了关于JVM中内存管理的基本知识,接下来本博客将带领大家了解java中的垃圾回收与内存分配策略. 垃 ...

  4. 每日一问:讲讲 Java 虚拟机的垃圾回收

    昨天我们用比较精简的文字讲了 Java 虚拟机结构,没看过的可以直接从这里查看: 每日一问:你了解 Java 虚拟机结构么? 今天我们必须来看看 Java 虚拟机的垃圾回收算法是怎样的.不过在开始之前 ...

  5. 高吞吐低延迟Java应用的垃圾回收优化

    高吞吐低延迟Java应用的垃圾回收优化 高性能应用构成了现代网络的支柱.LinkedIn有许多内部高吞吐量服务来满足每秒数千次的用户请求.要优化用户体验,低延迟地响应这些请求非常重要. 比如说,用户经 ...

  6. java中存在垃圾回收机制,但是还会有内存泄漏的问题,原因是

    答案是肯定的,但不能拿这一句回答面试官的问题.分析:JAVA是支持垃圾回收机制的,在这样的一个背景下,内存泄露又被称为“无意识的对象保持”.如果一个对象引用被无意识地保留下来,那么垃圾回收器不仅不会处 ...

  7. java虚拟机之垃圾回收算法

    标记-清除算法: 这是最基础的,就是之前所讲的两次标记,首先标记出所有 需要回收的对象,然后进行统一清除, 这有两缺点:一是效率低,标记和清除(开启低优先级进行回收)都是低效率的.第二是空间问题,标记 ...

  8. Java中的垃圾回收

    关于垃圾回收,主要是两个步骤: 垃圾对象的判断 垃圾对象的回收 垃圾对象的判断方法 引用计数算法:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何 ...

  9. 深入理解java虚拟机【垃圾回收算法】

    Java虚拟机的内存区域中,程序计数器.虚拟机栈和本地方法栈三个区域是线程私有的,随线程生而生,随线程灭而灭:栈中的栈帧随着方法的进入和退出而进行入栈和出栈操作,每个栈帧中分配多少内存基本上是在类结构 ...

随机推荐

  1. toogle

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. nginx设置反向代理后端jenklins,页面上的js css文件无法加载

    转载 2017年06月14日 22:36:59 8485 问题现象: nginx配置反向代理后,网页可以正常访问,但是页面上的js css文件无法加载,页面样式乱了. (1)nginx配置如下: (2 ...

  3. 包含jdk和nginx的基础镜像

    目的 制作一个基础镜像,包含jdk和nginx,这样要将java项目或一些前端页面做成容器,可以稍作修改引用该镜像. Dockerfile FROM alpine:3.8 ENV \ LANG=C.U ...

  4. python3 操作MYSQL实例及异常信息处理--用traceback模块

    # 用traceback模块查看异常import traceback import pymysql db = pymysql.connect(host='localhost', user='root' ...

  5. php7连接 sqlserver踩过的坑,could not find driver解决方式

    最近把环境升级为php7发现在连接sqlser的时候无法使用驱动了 页面错误 后来查看文档发现:php7应该采用Server=xxxx;DataBase=xxxxx 解决方式: DB_DSN_TWO ...

  6. 浅析 Bigtable 和 LevelDB 的实现

    在 2006 年的 OSDI 上,Google 发布了名为 Bigtable: A Distributed Storage System for Structured Data 的论文,其中描述了一个 ...

  7. C# 实现Bresenham算法(vs2010)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 〖C语言学习笔记 〗(一) HelloWorld

    前言 本文为c基础入门学习笔记 正文 HelloWorld #include <stdio.h> //标准输出流 int main() //每种语言都有一个执行入口,main方法就是其一 ...

  9. Python官方操作Excel文档

    xlwt 1.3.0 Downloads ↓ Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 ...

  10. 二、Internet地址结构

    IP路由器实现的转发程序使用IP地址来识别流量去向.IP地址也表示流量来源. 2.1 IP地址的表示 IPV4地址通常采用点分四组或点分十进制表示法,如192.168.1.1. 点分四组表示法由四个用 ...