【Java-38】基于Java的WeakHashMap垃圾回收

package Weakmap;

import java.util.WeakHashMap;

public class About_weakmap {

public static void main(String[] args) {
	WeakHashMap map=new WeakHashMap();
	//常量池不会被回收
	map.put("eqwe", "dwed");
	map.put("eqytwe", "dwed");
	map.put("eewqwe", "dwed");
	map.put("eqfwwe", "dwed");
	map.put("eqfswe", "dwed");
	map.put("eqwfewe", "dwed");
	map.put("eqferwe", "dwed");
	
	
	//会被回收
	map.put(new String("ertqwe"), new String("eqwe"));
	map.put(new String("eq,kwe"), new String("eqwe"));
	
	//回收命令
	System.gc();
	System.runFinalization();
	
	System.out.println(map.size());
	
}

}

 

你可能感兴趣的:(Java)