Common memory overflow in java

Source: Internet
Author: User

Common memory overflow in java
VM Stack Overflow (if the VM cannot apply for sufficient memory space during expansion, an OutOfMemoryError will be thrown)

Package com. jvm. memory; import java. util. arrayList; import java. util. list; public class HeapOOM {/*** VM runtime parameter-Xms20m-Xmx20m-XX: + HeapDumpOnOutOfMemoryError * @ param args */public static void main (String [] args) {List
 
  
List = new ArrayList
  
   
(); While (true) {list. add (new OOMObject () ;}} static class OOMObject {}}
  
 

Java. lang. OutOfMemoryError: Java heap space
Dumping heap to java_pid2076.hprof...
Exception in thread "main" Heap dump file created [24777740 bytes in 1.181 secs]
Java. lang. OutOfMemoryError: Java heap space
At java. util. Arrays. copyOf (Arrays. java: 2760)
At java. util. Arrays. copyOf (Arrays. java: 2734)
At java. util. ArrayList. ensureCapacity (ArrayList. java: 167)
At java. util. ArrayList. add (ArrayList. java: 351)
At com. jvm. memory. HeapOOM. main (HeapOOM. java: 14)
Java heap is used to store object instances. As long as we constantly create objects and reach the maximum capacity limit, the heap memory will overflow,
If this happens in the system, we need to analyze the snapshots dumped by dump using the image analysis tool to see if these classes have been created.

Required. Check from two aspects: 1. check whether there is a problem with the parameter settings and whether it can be increased. 2: Check whether the lifecycle of some objects is too long.

 

 

Local method Stack Overflow (if the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine itself, StackOverflowError will be thrown)

 

package com.jvm.memory;public class JavaJVMStackOOM {/** * VM args -Xss128k * @param args */public static void main(String[] args) {JavaJVMStackOOM oom = new JavaJVMStackOOM();oom.stackLeak();}private int stackLenth=1;public void stackLeak(){stackLenth++;stackLeak();}}

Exception in thread "main" java. lang. StackOverflowError
At com. jvm. memory. JavaJVMStackOOM. stackLeak (JavaJVMStackOOM. java: 16)
At com. jvm. memory. JavaJVMStackOOM. stackLeak (JavaJVMStackOOM. java: 17)

 

 

Constant pool overflow

 

package com.jvm.memory;import java.util.ArrayList;import java.util.List;public class RuntimeConstantPoolOOM {/** * VM Args -XX:PermSize=10 -XX:MaxPermSize=10 * @param args */public static void main(String[] args) {List
 
   list = new ArrayList
  
   ();int i=0;while(true){list.add(String.valueOf(i++).intern());}}}
  
 

Error occurred during initialization of VM
Too small initial permanent heap

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.