hard disk map

/*
* 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 test.
* @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("Hello Hello");
String buf2=new StringBuffer("Hello Hello").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();
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326224604&siteId=291194637