日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

聊聊jvm的-XX:MaxDirectMemorySize

發(fā)布時間:2024/4/14 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聊聊jvm的-XX:MaxDirectMemorySize 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文主要研究一下jvm的-XX:MaxDirectMemorySize

-XX:MaxDirectMemorySize

-XX:MaxDirectMemorySize=size用于設(shè)置New I/O(java.nio) direct-buffer allocations的最大大小,size的單位可以使用k/K、m/M、g/G;如果沒有設(shè)置該參數(shù)則默認(rèn)值為0,意味著JVM自己自動給NIO direct-buffer allocations選擇最大大小

System.initPhase1

java.base/java/lang/System.java

public final class System {/* Register the natives via the static initializer.** VM will invoke the initializeSystemClass method to complete* the initialization for this class separated from clinit.* Note that to use properties set by the VM, see the constraints* described in the initializeSystemClass method.*/private static native void registerNatives();static {registerNatives();}/** Don't let anyone instantiate this class */private System() {}/*** Initialize the system class. Called after thread initialization.*/private static void initPhase1() {// VM might invoke JNU_NewStringPlatform() to set those encoding// sensitive properties (user.home, user.name, boot.class.path, etc.)// during "props" initialization.// The charset is initialized in System.c and does not depend on the Properties.Map<String, String> tempProps = SystemProps.initProperties();VersionProps.init(tempProps);// There are certain system configurations that may be controlled by// VM options such as the maximum amount of direct memory and// Integer cache size used to support the object identity semantics// of autoboxing. Typically, the library will obtain these values// from the properties set by the VM. If the properties are for// internal implementation use only, these properties should be// masked from the system properties.//// Save a private copy of the system properties object that// can only be accessed by the internal implementation.VM.saveProperties(tempProps);props = createProperties(tempProps);StaticProperty.javaHome(); // Load StaticProperty to cache the property valueslineSeparator = props.getProperty("line.separator");FileInputStream fdIn = new FileInputStream(FileDescriptor.in);FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);setIn0(new BufferedInputStream(fdIn));setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));// Setup Java signal handlers for HUP, TERM, and INT (where available).Terminator.setup();// Initialize any miscellaneous operating system settings that need to be// set for the class libraries. Currently this is no-op everywhere except// for Windows where the process-wide error mode is set before the java.io// classes are used.VM.initializeOSEnvironment();// The main thread is not added to its thread group in the same// way as other threads; we must do it ourselves here.Thread current = Thread.currentThread();current.getThreadGroup().add(current);// register shared secretssetJavaLangAccess();// Subsystems that are invoked during initialization can invoke// VM.isBooted() in order to avoid doing things that should// wait until the VM is fully initialized. The initialization level// is incremented from 0 to 1 here to indicate the first phase of// initialization has completed.// IMPORTANT: Ensure that this remains the last initialization action!VM.initLevel(1);}//...... } 復(fù)制代碼

System的initPhase1方法會調(diào)用VM.saveProperties(tempProps)方法來保存一份系統(tǒng)配置供內(nèi)部實現(xiàn)使用;其中tempProps為SystemProps.initProperties()

jvm.cpp

hotspot/share/prims/jvm.cpp

// Convert the -XX:MaxDirectMemorySize= command line flag// to the sun.nio.MaxDirectMemorySize property.// Do this after setting user properties to prevent people// from setting the value with a -D option, as requested.// Leave empty if not suppliedif (!FLAG_IS_DEFAULT(MaxDirectMemorySize)) {char as_chars[256];jio_snprintf(as_chars, sizeof(as_chars), JULONG_FORMAT, MaxDirectMemorySize);Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.nio.MaxDirectMemorySize", CHECK_NULL);Handle value_str = java_lang_String::create_from_platform_dependent_str(as_chars, CHECK_NULL);result_h->obj_at_put(ndx * 2, key_str());result_h->obj_at_put(ndx * 2 + 1, value_str());ndx++;} 復(fù)制代碼

jvm.cpp里頭有一段代碼用于把-XX:MaxDirectMemorySize命令參數(shù)轉(zhuǎn)換為key為sun.nio.MaxDirectMemorySize的屬性

VM.saveProperties

java.base/jdk/internal/misc/VM.java

public class VM {// the init level when the VM is fully initializedprivate static final int JAVA_LANG_SYSTEM_INITED = 1;private static final int MODULE_SYSTEM_INITED = 2;private static final int SYSTEM_LOADER_INITIALIZING = 3;private static final int SYSTEM_BOOTED = 4;private static final int SYSTEM_SHUTDOWN = 5;// 0, 1, 2, ...private static volatile int initLevel;private static final Object lock = new Object();//......// A user-settable upper limit on the maximum amount of allocatable direct// buffer memory. This value may be changed during VM initialization if// "java" is launched with "-XX:MaxDirectMemorySize=<size>".//// The initial value of this field is arbitrary; during JRE initialization// it will be reset to the value specified on the command line, if any,// otherwise to Runtime.getRuntime().maxMemory().//private static long directMemory = 64 * 1024 * 1024;// Returns the maximum amount of allocatable direct buffer memory.// The directMemory variable is initialized during system initialization// in the saveAndRemoveProperties method.//public static long maxDirectMemory() {return directMemory;}//......// Save a private copy of the system properties and remove// the system properties that are not intended for public access.//// This method can only be invoked during system initialization.public static void saveProperties(Map<String, String> props) {if (initLevel() != 0)throw new IllegalStateException("Wrong init level");// only main thread is running at this time, so savedProps and// its content will be correctly published to threads started laterif (savedProps == null) {savedProps = props;}// Set the maximum amount of direct memory. This value is controlled// by the vm option -XX:MaxDirectMemorySize=<size>.// The maximum amount of allocatable direct buffer memory (in bytes)// from the system property sun.nio.MaxDirectMemorySize set by the VM.// If not set or set to -1, the max memory will be used// The system property will be removed.String s = props.get("sun.nio.MaxDirectMemorySize");if (s == null || s.isEmpty() || s.equals("-1")) {// -XX:MaxDirectMemorySize not given, take defaultdirectMemory = Runtime.getRuntime().maxMemory();} else {long l = Long.parseLong(s);if (l > -1)directMemory = l;}// Check if direct buffers should be page aligneds = props.get("sun.nio.PageAlignDirectMemory");if ("true".equals(s))pageAlignDirectMemory = true;}//...... } 復(fù)制代碼

VM的saveProperties方法讀取sun.nio.MaxDirectMemorySize屬性,如果為null或者是空或者是-1,那么則設(shè)置為Runtime.getRuntime().maxMemory();如果有設(shè)置MaxDirectMemorySize且值大于-1,那么使用該值作為directMemory的值;而VM的maxDirectMemory方法則返回的是directMemory的值

獲取maxDirectMemory的值

實例

public BufferPoolMXBean getDirectBufferPoolMBean(){return ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class).stream().filter(e -> e.getName().equals("direct")).findFirst().orElseThrow();}public JavaNioAccess.BufferPool getNioBufferPool(){return SharedSecrets.getJavaNioAccess().getDirectBufferPool();}/*** -XX:MaxDirectMemorySize=60M*/@Testpublic void testGetMaxDirectMemory(){ByteBuffer.allocateDirect(25*1024*1024);System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024.0);System.out.println(VM.maxDirectMemory() / 1024.0 / 1024.0);System.out.println(getDirectBufferPoolMBean().getTotalCapacity() / 1024.0 / 1024.0);System.out.println(getNioBufferPool().getTotalCapacity() / 1024.0 / 1024.0);} 復(fù)制代碼

輸出

輸出結(jié)果如下:

4096.0 60.0 25.0 25.0 復(fù)制代碼
  • 由于java9模塊化之后,VM從原來的sun.misc.VM變更到j(luò)ava.base模塊下的jdk.internal.misc.VM;上面代碼默認(rèn)是unamed module,要使用jdk.internal.misc.VM就需要使用--add-exports java.base/jdk.internal.misc=ALL-UNNAMED將其導(dǎo)出到UNNAMED,這樣才可以運行
  • 同理java9模塊化之后,SharedSecrets從原來的sun.misc.SharedSecrets變更到j(luò)ava.base模塊下的jdk.internal.access.SharedSecrets;要使用--add-exports java.base/jdk.internal.access=ALL-UNNAMED將其導(dǎo)出到UNNAMED,這樣才可以運行
  • 從輸出結(jié)果可以看出,Runtime.getRuntime().maxMemory()輸出的值是正確的,而BufferPoolMXBean及JavaNioAccess.BufferPool的getTotalCapacity返回的都是directBuffer大小,而非max值

使用API查看directBuffer使用情況

實例

/*** -XX:MaxDirectMemorySize=60M*/@Testpublic void testGetDirectMemoryUsage(){ByteBuffer.allocateDirect(30*1024*1024);System.out.println(getDirectBufferPoolMBean().getMemoryUsed() / 1024.0 / 1024.0);System.out.println(getNioBufferPool().getMemoryUsed() / 1024.0 / 1024.0);} 復(fù)制代碼

輸出

輸出結(jié)果如下:

30.0 30.0 復(fù)制代碼

可以看到BufferPoolMXBean及JavaNioAccess.BufferPool的getMemoryUsed可以返回directBuffer大小

OOM

java.lang.OutOfMemoryError: Direct buffer memoryat java.base/java.nio.Bits.reserveMemory(Bits.java:175)at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:118)at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:317) 復(fù)制代碼

如果上面的ByteBuffer.allocateDirect改為分配超過60M,則運行拋出OutOfMemoryError

使用NMT查看directBuffer使用情況

jcmd 3088 VM.native_memory scale=MB 3088:Native Memory Tracking:Total: reserved=5641MB, committed=399MB - Java Heap (reserved=4096MB, committed=258MB)(mmap: reserved=4096MB, committed=258MB)- Class (reserved=1032MB, committed=6MB)(classes #1609)( instance classes #1460, array classes #149)(mmap: reserved=1032MB, committed=5MB)( Metadata: )( reserved=8MB, committed=5MB)( used=3MB)( free=2MB)( waste=0MB =0.00%)( Class space:)( reserved=1024MB, committed=1MB)( used=0MB)( free=0MB)( waste=0MB =0.00%)- Thread (reserved=18MB, committed=18MB)(thread #18)(stack: reserved=18MB, committed=18MB)- Code (reserved=242MB, committed=7MB)(mmap: reserved=242MB, committed=7MB)- GC (reserved=203MB, committed=60MB)(malloc=18MB #2443)(mmap: reserved=185MB, committed=43MB)- Internal (reserved=1MB, committed=1MB)(malloc=1MB #1257)- Other (reserved=30MB, committed=30MB)(malloc=30MB #2)- Symbol (reserved=1MB, committed=1MB)(malloc=1MB #13745)- Shared class space (reserved=17MB, committed=17MB)(mmap: reserved=17MB, committed=17MB) 復(fù)制代碼

從Other部分可以看到其值跟ByteBuffer.allocateDirect使用的值一致,改變ByteBuffer.allocateDirect的值再重新查看,可以發(fā)現(xiàn)Other部分跟著改變;因而初步斷定Other部分應(yīng)該是可以反映direct memory的使用大小

小結(jié)

  • -XX:MaxDirectMemorySize=size用于設(shè)置New I/O(java.nio) direct-buffer allocations的最大大小,size的單位可以使用k/K、m/M、g/G;如果沒有設(shè)置該參數(shù)則默認(rèn)值為0,意味著JVM自己自動給NIO direct-buffer allocations選擇最大大小;從代碼java.base/jdk/internal/misc/VM.java中可以看到默認(rèn)是取的Runtime.getRuntime().maxMemory()
  • 使用jdk.internal.misc.VM.maxDirectMemory()可以獲取maxDirectMemory的值;由于java9模塊化之后,VM從原來的sun.misc.VM變更到j(luò)ava.base模塊下的jdk.internal.misc.VM;上面代碼默認(rèn)是unamed module,要使用jdk.internal.misc.VM就需要使用--add-exports java.base/jdk.internal.misc=ALL-UNNAMED將其導(dǎo)出到UNNAMED,這樣才可以運行
  • BufferPoolMXBean及JavaNioAccess.BufferPool(通過SharedSecrets獲取)的getMemoryUsed可以獲取direct memory的大小;其中java9模塊化之后,SharedSecrets從原來的sun.misc.SharedSecrets變更到j(luò)ava.base模塊下的jdk.internal.access.SharedSecrets;要使用--add-exports java.base/jdk.internal.access=ALL-UNNAMED將其導(dǎo)出到UNNAMED,這樣才可以運行

另外使用NMT也可以查看direct memory的使用情況,其包含在了Other部分

doc

  • Default HotSpot Maximum Direct Memory Size
  • JVM源碼分析之堆外內(nèi)存完全解讀
  • Java堆外內(nèi)存增長問題排查Case
  • DirectByteBuffer堆外內(nèi)存溢出問題排查
  • Default for XX:MaxDirectMemorySize
  • java.lang.OutOfMemoryError: Direct buffer memory (with tcnative) #6813
  • VM.java MaxDirectMemorySize
  • Summary of understanding "-XX:MaxDirectMemorySize" setting.
  • native-mem-tracking.md
  • Java Platform, Standard Edition Tools Reference

轉(zhuǎn)載于:https://juejin.im/post/5c9ced366fb9a070e344c614

總結(jié)

以上是生活随笔為你收集整理的聊聊jvm的-XX:MaxDirectMemorySize的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。