Resin关于最小内存设置

 

resin.conf 中有 <min-free-memory>设置,是resin内部有一个thread每隔10秒钟检查JVM剩余内存,如果free memory小于 2*min-free-memory,则执行Runtime.gc() 强制进行GC,如果使用-XX:+DisableExplicitGC 禁用显式调用GC,则该设置无效。

对应的代码如下(com/caucho/server/resin/Resin.java):

long minFreeMemory = _server.getMinFreeMemory(); if (minFreeMemory <= 0) { // memory check disabled } else if (2 * minFreeMemory < getFreeMemory(runtime)) { // plenty of free memory } else { if (log().isLoggable(Level.FINER)) { log().finer(L().l("free memory {0} max:{1} total:{2} free:{3}", "" + getFreeMemory(runtime), "" + runtime.maxMemory(), "" + runtime.totalMemory(), "" + runtime.freeMemory())); } log().info(L().l("Forcing GC due to low memory. {0} free bytes.", getFreeMemory(runtime))); runtime.gc(); if (getFreeMemory(runtime) < minFreeMemory) { _isClosed = true; log().severe(L().l("Restarting due to low free memory. {0} free bytes", getFreeMemory(runtime))); return; }

其中getMinFreeMemory()如下:

private static long getFreeMemory(Runtime runtime) { long maxMemory = runtime.maxMemory(); long totalMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); // Some JDKs (JRocket) return 0 for the maxMemory if (maxMemory < totalMemory) return freeMemory; else return maxMemory - totalMemory + freeMemory; } 

计算jvm空余内存的方法值得注意一下:由于JVM可以使用-Xms 和-Xmx 指定启动内存和最大内存,所以计算空余内存的时候,需要考虑到JVM可以分配的最大内存(runtime.maxMemory())和当前可使用的最大内存(runtime.totalMemory()),因此最终的free memory =当前空余内存(runtime.freeMemory())  + 可以分配但还没有分配的内存(runtime.maxMemory() - runtime.totalMemory())

 

--EOF--

 

 

你可能感兴趣的:(jvm,thread)