提交 56b9a1a2 authored 作者: Thomas Mueller's avatar Thomas Mueller

Remove dependency from Constants to SysProperties.

上级 0e15d614
......@@ -157,9 +157,9 @@ public class Constants {
public static final int CACHE_MIN_RECORDS = 16;
/**
* The default cache size in KB.
* The default cache size in KB for each GB of RAM.
*/
public static final int CACHE_SIZE_DEFAULT = SysProperties.scalePropertyForAvailableMemory(64 * 1024);
public static final int CACHE_SIZE_DEFAULT = 64 * 1024;
/**
* The default cache type.
......
......@@ -206,9 +206,12 @@ public class Database implements DataHandler {
ci.getProperty("ACCESS_MODE_DATA", "rw"));
this.autoServerMode = ci.getProperty("AUTO_SERVER", false);
this.autoServerPort = ci.getProperty("AUTO_SERVER_PORT", 0);
int defaultCacheSize = Utils.scaleForAvailableMemory(
Constants.CACHE_SIZE_DEFAULT);
this.cacheSize =
ci.getProperty("CACHE_SIZE", Constants.CACHE_SIZE_DEFAULT);
this.pageSize = ci.getProperty("PAGE_SIZE", Constants.DEFAULT_PAGE_SIZE);
ci.getProperty("CACHE_SIZE", defaultCacheSize);
this.pageSize = ci.getProperty("PAGE_SIZE",
Constants.DEFAULT_PAGE_SIZE);
if ("r".equals(accessModeData)) {
readOnly = true;
}
......
......@@ -5,10 +5,6 @@
*/
package org.h2.engine;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.h2.util.MathUtils;
import org.h2.util.Utils;
......@@ -460,6 +456,10 @@ public class SysProperties {
*/
public static final boolean USE_THREAD_CONTEXT_CLASS_LOADER =
Utils.getProperty("h2.useThreadContextClassLoader", false);
static {
System.out.println("init sysprop");
}
/**
* System property <code>h2.serializeJavaObject</code>
......@@ -552,29 +552,7 @@ public class SysProperties {
// ignore
}
}
return scalePropertyForAvailableMemory(defaultValue);
return Utils.scaleForAvailableMemory(defaultValue);
}
public static int scalePropertyForAvailableMemory(int defaultValue) {
long maxMemory = Runtime.getRuntime().maxMemory();
if (maxMemory != Long.MAX_VALUE) {
// we are limited by an -XmX parameter
return (int) (defaultValue * maxMemory / (1024 * 1024 * 1024));
}
try {
OperatingSystemMXBean mxBean = ManagementFactory
.getOperatingSystemMXBean();
// this method is only available on the class com.sun.management.OperatingSystemMXBean, which mxBean
// is an instance of under the Oracle JDK, but it is not present on Android and other JDK's
Method method = Class.forName(
"com.sun.management.OperatingSystemMXBean").
getMethod("getTotalPhysicalMemorySize");
long physicalMemorySize = ((Number) method.invoke(mxBean)).longValue();
return (int) (defaultValue * physicalMemorySize / (1024 * 1024 * 1024));
} catch (Exception e) {
// ignore
}
return defaultValue;
}
}
......@@ -8,6 +8,8 @@ package org.h2.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
......@@ -734,6 +736,35 @@ public class Utils {
}
return defaultValue;
}
/**
* Scale the value with the available memory. If 1 GB of RAM is available,
* the value is returned, if 2 GB are available, then twice the value, and so on.
*
* @param value the value to scale
* @return the scaled value
*/
public static int scaleForAvailableMemory(int value) {
long maxMemory = Runtime.getRuntime().maxMemory();
if (maxMemory != Long.MAX_VALUE) {
// we are limited by an -XmX parameter
return (int) (value * maxMemory / (1024 * 1024 * 1024));
}
try {
OperatingSystemMXBean mxBean = ManagementFactory
.getOperatingSystemMXBean();
// this method is only available on the class com.sun.management.OperatingSystemMXBean, which mxBean
// is an instance of under the Oracle JDK, but it is not present on Android and other JDK's
Method method = Class.forName(
"com.sun.management.OperatingSystemMXBean").
getMethod("getTotalPhysicalMemorySize");
long physicalMemorySize = ((Number) method.invoke(mxBean)).longValue();
return (int) (value * physicalMemorySize / (1024 * 1024 * 1024));
} catch (Exception e) {
// ignore
}
return value;
}
/**
* The utility methods will try to use the provided class factories to
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论