Configure virtual machine parameters in idea and virtual machine memory overflow

Java heap overflow

The Java heap is used to store object instances. If the number of objects increases to the maximum capacity of the heap, memory overflow will occur.

The minimum value of the heap-Xms The maximum value of the heap-Xmx

Idea configure virtual machine parameters

idea
Set virtual machine parameters

    static class OOMObject
    {
    
    

    }

    public static void main(String[] args)
    {
    
    
        List<OOMObject> list = new ArrayList<>();
        while (true)
        {
    
    
            list.add(new OOMObject());
        }
    }

operation result

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid16012.hprof ...
Heap dump file created [29135766 bytes in 0.072 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at Scratch.main(scratch.java:44)

Virtual machine stack and local method stack overflow

The parameter -Xss modifies the stack capacity.

  1. If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, a "StackOverflowError" exception will be thrown.
  2. If the memory of the virtual machine stack allows dynamic expansion, when the expansion stack capacity cannot apply for enough memory, an OutOfMemoryError exception will be thrown.

Method area and runtime constant pool overflow

The runtime constant pool is part of the method area.
The virtual machine parameters -XX:PermSize, -XX:MaxPermSize limit the size of the permanent generation.
In JDK6 and earlier HotSpot virtual machines, the constant pool is allocated in the permanent generation, and the constant pool of JDK7 and above is allocated in the Java heap. So before JDK6, by limiting the size of the permanent generation, the virtual machine can throw an OutOfMemoryError exception. After JDK7, the heap space must be limited.
String::intern() : If the string constant pool already contains a string equal to this String object, it returns a reference to the String object representing the string in the pool; otherwise, it will return the characters contained in this String object The string is added to the constant pool, and a reference to this String object is returned.

 public static void main(String[] args)
    {
    
    
        String str1 = new StringBuilder("计算机").append("网络工程").toString();
        System.out.println(str1.intern() == str1);

        String str2 = new StringBuilder("ja").append("va").toString();
        System.out.println(str2.intern() == str2);
    }
返回结果:
JDK7及以上
true
false
JDK6
false
false

Reason: In JDK6, the string encountered for the first time will be stored in the permanent generation, and the return is a reference in the permanent generation, and StringBuilder is an object created in the Java heap, so it is not a reference.
The constant pool of JDK7 or above has been moved to the Java heap. You only need to record the first occurrence of the reference instance. Therefore, the reference returned by intern() and the string instance created by StringBuilder are used. str2 returns false because java characters are not encountered for the first time (loaded into the constant pool when loading the sun.misc.Version class).
After JDK8, the metaspace replaces the permanent generation.
The parameter -XX:MaxMetaspaceSize sets the maximum value of the metaspace. The default value is -1, which means there is no limit, only limited to local memory.
-XX:MetaspaceSize: Specify the initial size of the metaspace, in bytes. When this value is reached, garbage collection will be triggered for type unloading, and the collector will adjust the value. If a large amount of space is released, the value should be appropriately lowered, and if a small amount of space is released, it can be appropriately reduced.
-XX:MinMetaspaceFreeRatio: The function is to control the percentage of the remaining capacity of the smallest metaspace after garbage collection, which can reduce the frequency of garbage collection due to insufficient metaspace.

Native direct memory overflow

Direct memory is specified by -XX:ManDirectMemorySize. Do not specify the default and heap memory always (-Xmx)

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109323425