Java memory overflow and java Overflow

Source: Internet
Author: User
Tags xms

Java memory overflow and java Overflow

1. java Heap Overflow

JAVA heap is used to store object instances. As long as you constantly create objects and ensure that GC Roots directly have accessible paths to objects, so that the garbage collection mechanism does not know the objects clearly, memory overflow may occur.

The following code limits the Java heap size to 1024 MB, and the-Xms and-Xmx parameters are the same, that is, they cannot be expanded. Add VM parameters to Run Configuration:

 
 
  1. -XX:+HeapDumpOnOutOfMemoryError

Let the VM Dump out the current memory heap snapshot when a memory overflow exception occurs.

 
 
  1. package com.jvm.exception;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class HeapSpaceException {
  5. public static void main(String[] args) {
  6. List<byte[]> byteList = new ArrayList<byte[]>();
  7. while(true){
  8. byteList.add(new byte[1024]);
  9. }
  10. }
  11. }

Running result:

 
 
  1. java.lang.OutOfMemoryError: Java heap space
  2. Dumping heap to java_pid2783.hprof ...
  3. Heap dump file created [3795151132 bytes in 3.289 secs]
  4. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  5. at com.jvm.exception.HeapSpaceException.main(HeapSpaceException.java:13)

In the memory, the system first prompts an OutOfMemoryError to upgrade the Java heap space.

Generally, Memory analysis tools such as Eclipse Memory Analyzer can be used for analysis.

If the memory leaks, view the reference chain of the leaked object to GC Roots to determine the memory leak location.

If the memory is not leaked, check the virtual machine parameters (-Xmx and-Xms ).

2. Virtual Machine stack and local method Stack Overflow

Because the HotSpot virtual machine does not distinguish between the virtual machine stack and the local method stack, the-Xoss (set the local method stack size) parameter exists for the HotSpot, but it is actually invalid. Stack capacity can only be set using the-Xss parameter. The Virtual Machine stack and local method stack define two exceptions in the Java Virtual Machine specification:

-1. If the thread request stack depth is greater than the maximum depth allowed by the virtual machine, an StackOverFlowError error is thrown.

-2. If the VM cannot apply for sufficient memory space when looking for an extended stack, an OutOfMmemoryError is thrown.

First, in a single-threaded environment, the-Xss parameter is used to reduce the stack memory capacity and StackOverFlowError is thrown.

 
 
  1. package com.jvm.exception;
  2. /**
  3. * Default-Xss Parameter
  4. * @author jinglongjun
  5. *
  6. */
  7. public class StackOverFlowError {
  8. private int stackLength = 1;
  9. public void stackLeak(){
  10. stackLength ++;
  11. stackLeak();
  12. }
  13. public static void main(String[] args) {
  14. StackOverFlowError sof = new StackOverFlowError();
  15. try{
  16. sof.stackLeak();
  17. }catch(Throwable e){
  18. System.out.println("stackLength : " + sof.stackLength);
  19. throw e;
  20. }
  21. }
  22. }

Output content:

 
 
  1. stackLength : 10828
  2. Exception in thread "main" java.lang.StackOverflowError
  3. at com.jvm.exception.StackOverFlowError.stackLeak(StackOverFlowError.java:13)
  4. at com.jvm.exception.StackOverFlowError.stackLeak(StackOverFlowError.java:14)

The single-threaded Environment defines a large number of local variables and increases the length of the local variable table in the method stack. When StackOverFlowEoor is thrown, the output stack depth is reduced accordingly.

This situation has not been simulated for a long time. This is because this article refers to the book "deep understanding of Java Virtual Machine", which does not provide examples.

StackOverFlowError is always thrown in a single thread.

Multi-thread simulation.

We recommend that you do not try to run the following code.

We recommend that you do not try to run the following code.

We recommend that you do not try to run the following code.

 
 
  1. package com.jvm.exception;
  2. public class OutOfMemoryError {
  3. public void dontStop(){
  4. while(true){
  5. }
  6. }
  7. public void stackLengthThread(){
  8. while(true){
  9. Thread thread = new Thread(new Runnable() {
  10. @Override
  11. public void run() {
  12. dontStop();
  13. }
  14. });
  15. thread.start();
  16. }
  17. }
  18. public static void main(String[] args) {
  19. OutOfMemoryError oome = new OutOfMemoryError();
  20. oome.stackLengthThread();
  21. }
  22. }

The above code will throw:

OutOfMemoryError.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.