提交 6f10091a authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

Auto-scale MAX_MEMORY_ROWS and CACHE_SIZE settings by the amount of available RAM.

Gives a better out of box experience for people with more powerful machines.
上级 a44b3bcf
......@@ -1057,7 +1057,7 @@ SET AUTOCOMMIT OFF
SET CACHE_SIZE int
","
Sets the size of the cache in KB (each KB being 1024 bytes) for the current database.
The default value is 16384 (16 MB). The value is rounded to the next higher power of two.
The default value is = 65536 * available_RAM_in_G, i.e. 64 MB per G. The value is rounded to the next higher power of two.
Depending on the virtual machine, the actual memory required may be higher.
This setting is persistent and affects all connections as there is only one cache per database.
......@@ -1358,7 +1358,8 @@ SET MAX_LOG_SIZE 2
SET MAX_MEMORY_ROWS int
","
The maximum number of rows in a result set that are kept in-memory. If more rows
are read, then the rows are buffered to disk. The default value is 10000.
are read, then the rows are buffered to disk.
The default value is 40000 per G of available RAM.
Admin rights are required to execute this command, as it affects all connections.
This command commits an open transaction.
......
......@@ -17,7 +17,9 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>change default value of PAGE_SIZE from 2048 to 4096 to more closely match most filesystems block size.
<ul><li>Cchange default value of PAGE_SIZE from 2048 to 4096 to more closely match most filesystems block size.
</li><li>Auto-scale MAX_MEMORY_ROWS and CACHE_SIZE settings by the amount of available RAM. Gives a better
out of box experience for people with more powerful machines.
</li></ul>
<h2>Version 1.4.180 Beta (2014-07-13)</h2>
......
......@@ -159,7 +159,7 @@ public class Constants {
/**
* The default cache size in KB.
*/
public static final int CACHE_SIZE_DEFAULT = 16 * 1024;
public static final int CACHE_SIZE_DEFAULT = SysProperties.scalePropertyForAvailableMemory(64 * 1024);
/**
* The default cache type.
......
......@@ -5,6 +5,10 @@
*/
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;
......@@ -250,11 +254,11 @@ public class SysProperties {
Utils.getProperty("h2.maxReconnect", 3);
/**
* System property <code>h2.maxMemoryRows</code> (default: 10000).<br />
* System property <code>h2.maxMemoryRows</code> (default: 40000 per G of available RAM).<br />
* The default maximum number of rows to be kept in memory in a result set.
*/
public static final int MAX_MEMORY_ROWS =
Utils.getProperty("h2.maxMemoryRows", 10000);
getAutoScaledForMemoryProperty("h2.maxMemoryRows", 40000);
/**
* System property <code>h2.maxTraceDataLength</code>
......@@ -534,5 +538,47 @@ public class SysProperties {
public static String getScriptDirectory() {
return Utils.getProperty(H2_SCRIPT_DIRECTORY, "");
}
/**
* This method attempts to auto-scale some of our properties to take advantage of more powerful machines out of the box.
* We assume that our default properties are set correctly for approx. 1G of memory, and scale them up if we have more.
*/
private static int getAutoScaledForMemoryProperty(String key, int defaultValue) {
String s = Utils.getProperty(key, null);
if (s != null) {
try {
return Integer.decode(s).intValue();
} catch (NumberFormatException e) {
// ignore
}
}
return scalePropertyForAvailableMemory(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));
}
OperatingSystemMXBean mxBean = ManagementFactory
.getOperatingSystemMXBean();
try {
// 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 = mxBean.getClass().getMethod("getTotalPhysicalMemorySize");
long physicalMemorySize = ((Number) method.invoke(mxBean)).longValue();
return (int) (defaultValue * physicalMemorySize / (1024 * 1024 * 1024));
} catch (NoSuchMethodException e) {
// ignore
} catch (IllegalArgumentException e) {
// ignore
} catch (IllegalAccessException e) {
// ignore
} catch (InvocationTargetException e) {
// ignore
}
return defaultValue;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论