---
title: "JAVA中使用引用计数技术来实现垃圾回收机制"
author: "Dreamsture"
date: "2015-01-21T15:05:56Z"
tags: ["Java", "引用计数"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2015-01-21T15:08:26Z"
id: 2178080
url: "https://dreamsture.iteye.com/blog/2178080"
---

根据自己对JAVA的学习理解，解释一下引用计数技术，有什么不对的请大家指正。以人吃苹果为例，代码如下：

```java

package com.test11;


/**
 * 测试对象的销毁
 * @author Administrator
 *
 */
public class Apple{
	/**
	 * 属性
	 */
	private int refCount = 0;//某个引用被使用的次数
	private static long counter = 0;//引用的个数
	private final long id = counter++;//引用的id
	
	/**
	 * 构造器
	 */
	public Apple(){
		System.out.println("买了个苹果\t apple"+id);
	}
	/**
	 * 某个引用，每被使用一次引用次数加一
	 */
	public void addRef(){
		refCount++;
	}
	/**
	 * 销毁对象
	 */
	protected void dispose(){
		if(--refCount==0){
			System.out.println("所有人吃完了苹果apple"+id+"，现在丢掉苹果核！");
			counter--;
		}else{
			System.out.println("其他人还没吃完苹果，苹果先不销毁！");
		}
	}
	/**
	 * get方法
	 */
	public int getRefCount() {
		return refCount;
	}
	public static long getCounter() {
		return counter;
	}
	public long getId() {
		return id;
	}
}


```

----------------------------------------------------------------

```java

package com.test11;


/**
 * 测试对象的销毁
 * @author Administrator
 *
 */
public class Person{
	/**
	 * 属性
	 */
	private Apple apple;
	private static long counter = 0;//引用的个数
	private final long id = counter++;//引用的id
	private String name;
	
	/**
	 * 构造器
	 */
	public Person(Apple apple,String name){
		this.apple = apple;
		this.name = name;
		System.out.println(name+"走进了小屋中!");
		this.apple.addRef();
	}
	/**
	 * 销毁对象
	 */
	protected void dispose(){
			System.out.println(name+"走出了小屋！");
			apple.dispose();
	}
	/**
	 * 吃苹果
	 */
	protected void eatApple(){
		System.out.println(name+"吃了一口苹果！");
	}
	/**
	 * get方法
	 */
//	public int getApple() {
//		return apple;
//	}
	public long getCounter() {
		return counter;
	}
	public long getId() {
		return id;
	}
}


```

----------------------------------------------------------------


```java

package com.test11;

/**
 * 使用引用计数技术对对象的销毁
 * 
 * @author Administrator
 * 
 */
public class Test {

	public static void main(String[] args) {
		/**
		 * 现在要在一个小屋内（内存）给人（对象）发苹果（对象）吃！
		 */
		// 先创建两个苹果
		Apple apple0 = new Apple();
		Apple apple1 = new Apple();
		System.out.println("**********************************************************");
		System.out.println("现在的苹果数量为："+Apple.getCounter());
		System.out.println("苹果apple0被引用的数量为："+apple0.getRefCount());
		System.out.println("苹果apple1被引用的数量为："+apple1.getRefCount());
		System.out.println("**********************************************************");
		// 创建一群人（五个人）,将苹果apple0发给这群人吃（每个人只能吃一口）
		Person[] persons = { new Person(apple0,"张三"), new Person(apple0,"李四"),
				new Person(apple0,"王五"), new Person(apple0,"赵六"), new Person(apple0,"刘齐") };
		
		System.out.println("**********************************************************");
		System.out.println("现在的苹果数量为："+Apple.getCounter());
		System.out.println("苹果apple0被引用的数量为："+apple0.getRefCount());
		System.out.println("苹果apple1被引用的数量为："+apple1.getRefCount());
		System.out.println("**********************************************************");
		
		for(Person p:persons){
			p.eatApple();
			p.dispose();//吃完苹果的人，要从小屋中出去（销毁对象释放内存）
		}
		System.out.println("**********************************************************");
		System.out.println("现在的苹果数量为："+Apple.getCounter());
		System.out.println("苹果apple0被引用的数量为："+apple0.getRefCount());
		System.out.println("苹果apple1被引用的数量为："+apple1.getRefCount());
		System.out.println("**********************************************************");
	}
}


```


=====================运行结果如下=========================
买了个苹果	 apple0
买了个苹果	 apple1
**********************************************************
现在的苹果数量为：2
苹果apple0被引用的数量为：0
苹果apple1被引用的数量为：0
**********************************************************
张三走进了小屋中!
李四走进了小屋中!
王五走进了小屋中!
赵六走进了小屋中!
刘齐走进了小屋中!
**********************************************************
现在的苹果数量为：2
苹果apple0被引用的数量为：5
苹果apple1被引用的数量为：0
**********************************************************
张三吃了一口苹果！
张三走出了小屋！
其他人还没吃完苹果，苹果先不销毁！
李四吃了一口苹果！
李四走出了小屋！
其他人还没吃完苹果，苹果先不销毁！
王五吃了一口苹果！
王五走出了小屋！
其他人还没吃完苹果，苹果先不销毁！
赵六吃了一口苹果！
赵六走出了小屋！
其他人还没吃完苹果，苹果先不销毁！
刘齐吃了一口苹果！
刘齐走出了小屋！
所有人吃完了苹果apple0，现在丢掉苹果核！
**********************************************************
现在的苹果数量为：1
苹果apple0被引用的数量为：0
苹果apple1被引用的数量为：0
**********************************************************