?? memoryutils.java
字號:
/*
* Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.util;
/**
* This is a utility class with functions to measure the free and used memory.
*/
public class MemoryUtils {
private static long lastGC;
private static final int GC_DELAY = 50;
private static final int MAX_GC = 8;
/**
* Get the used memory in KB.
*
* @return the used memory
*/
public static int getMemoryUsed() {
collectGarbage();
Runtime rt = Runtime.getRuntime();
long mem = rt.totalMemory() - rt.freeMemory();
return (int) (mem >> 10);
}
/**
* Get the free memory in KB.
*
* @return the used memory
*/
public static int getMemoryFree() {
collectGarbage();
Runtime rt = Runtime.getRuntime();
long mem = rt.freeMemory();
return (int) (mem >> 10);
}
private static synchronized void collectGarbage() {
Runtime runtime = Runtime.getRuntime();
long total = runtime.totalMemory();
long time = System.currentTimeMillis();
if (lastGC + GC_DELAY < time) {
for (int i = 0; i < MAX_GC; i++) {
runtime.gc();
long now = runtime.totalMemory();
if (now == total) {
lastGC = System.currentTimeMillis();
break;
}
total = now;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -