From f50e99ed18268b7cd0d66d4bdef0d0c481c0d6ad Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 29 Jan 2016 20:15:09 +0300 Subject: [PATCH] [platform] fixes direct access to internal JDK API --- .../intellij/util/io/PagedFileStorage.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/platform/util/src/com/intellij/util/io/PagedFileStorage.java b/platform/util/src/com/intellij/util/io/PagedFileStorage.java index 615f11110c8e..22b858974950 100644 --- a/platform/util/src/com/intellij/util/io/PagedFileStorage.java +++ b/platform/util/src/com/intellij/util/io/PagedFileStorage.java @@ -25,12 +25,12 @@ import com.intellij.util.containers.hash.LinkedHashMap; import jsr166e.extra.SequenceLock; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import sun.misc.VM; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.*; @@ -53,13 +53,32 @@ public class PagedFileStorage implements Forceable { final int upper = SystemInfo.is64Bit ? 500 : 200; BUFFER_SIZE = Math.max(1, SystemProperties.getIntProperty("idea.paged.storage.page.size", 10)) * MB; - final long max = VM.maxDirectMemory() - 2 * BUFFER_SIZE; + final long max = maxDirectMemory() - 2 * BUFFER_SIZE; LOWER_LIMIT = (int)Math.min(lower * MB, max); UPPER_LIMIT = (int)Math.min(Math.max(LOWER_LIMIT, SystemProperties.getIntProperty("idea.max.paged.storage.cache", upper) * MB), max); LOG.info("lower=" + (LOWER_LIMIT / MB) + "; upper=" + (UPPER_LIMIT / MB) + "; buffer=" + (BUFFER_SIZE / MB) + "; max=" + (max / MB)); } + private static long maxDirectMemory() { + try { + Class aClass = Class.forName("sun.misc.VM"); + Method maxDirectMemory = aClass.getMethod("maxDirectMemory"); + return (Long)maxDirectMemory.invoke(null); + } + catch (Throwable ignore) { } + + try { + Class aClass = Class.forName("java.nio.Bits"); + Field maxMemory = aClass.getDeclaredField("maxMemory"); + maxMemory.setAccessible(true); + return (Long)maxMemory.get(null); + } + catch (Throwable ignore) { } + + return Runtime.getRuntime().maxMemory(); + } + private static final int UNKNOWN_PAGE = -1; private static final int MAX_PAGES_COUNT = 0xFFFF; private static final int MAX_LIVE_STORAGES_COUNT = 0xFFFF;