欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

硬盘map

程序员文章站 2022-07-15 13:51:03
...
/*
* Copyright (c) 2011 Skyon Technology Ltd.
* All rights reserved.
*
* project: java1
* create: May 12, 2011 4:04:03 PM
* cvs: $Id: $
*/
package skyon.util.map;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.alok.diskmap.DiskBackedMap;
/**
* TODO map测试.
* @author xinglj 
* @version $Revision:$
*/
public class TestDiskBackedMap {
public static void main(String[] args) throws Throwable {
test1();
// test2();
}
/**
* @throws Throwable 
* 
*/
private static void test1() throws Throwable {
System.out.println("maxMemo "+Runtime.getRuntime().maxMemory()/1024000);
System.out.println("totalMemo "+Runtime.getRuntime().totalMemory()/1024000);
System.out.println("freeMemo "+Runtime.getRuntime().freeMemory()/1024000);
DiskBackedMap<String, String> diskBackedMap = new DiskBackedMap<String, String>("c:/temp");
Map<String, String> m= diskBackedMap;
testmap(m);

diskBackedMap.finalize();

}
private static void test2() throws Throwable {
System.out.println("maxMemo "+Runtime.getRuntime().maxMemory()/1024000);
System.out.println("totalMemo "+Runtime.getRuntime().totalMemory()/1024000);
System.out.println("freeMemo "+Runtime.getRuntime().freeMemory()/1024000);

try {
Map<String, String> m=
new HashMap<String, String>();
// diskBackedMap;
testmap(m);
} catch (Exception e) {

e.printStackTrace();
System.gc();
}



}
/**
* @param m
*/
private static void testmap(Map<String, String> m) {
int i=0;
try {
StringBuffer buf=new StringBuffer("你好你好");
String buf2=new StringBuffer("你好你好").toString();
System.out.println("begin to write...");
int length = 500000;
for(;i<length;i++){
m.put(""+i, new String(buf.toString()+i));
// buf2 +=buf2;
m.put(2*i+"", buf2);
}

System.out.println("begin to read...");
for(i=0;i<length;i++)
m.get(""+i);
System.out.println("read end.");
System.out.println("test ok?"+m.get("0").equals("你好你好0"));
System.out.println("test ok?"+m.get("20000").equals("你好你好20000"));
System.out.println("test ok?"+m.get("49999").equals("你好你好49999"));
} catch (OutOfMemoryError e) {
System.out.println("for loop to "+i);
e.printStackTrace();
}
System.out.println("maxMemo "+Runtime.getRuntime().maxMemory()/1024000);
System.out.println("totalMemo "+Runtime.getRuntime().totalMemory()/1024000);
System.out.println("freeMemo "+Runtime.getRuntime().freeMemory()/1024000);
m.clear();
}
}