java.lang.OutOfMemoryError:Java堆空间

java.lang.OutOfMemoryError is thrown when JVM is unable to allocate memory to create an object. Java OutOfMemoryError is an Error and occurs a runtime.

当JVM无法分配内存来创建对象时,抛出java.lang.OutOfMemoryError。 Java OutOfMemoryError是一个错误,并且在运行时发生。

java.lang.OutOfMemoryError (java.lang.OutOfMemoryError)

As the name suggests, OutOfMemoryError occurs when java runtime is out of memory. In this case garbage collector is unable to free more space required by program and hence error is thrown.

顾名思义,当Java运行时内存不足时,会发生OutOfMemoryError 。 在这种情况下,垃圾收集器无法释放程序所需的更多空间,因此会引发错误。

There are two main reasons to get java.lang.OutOfMemoryError:

获得java.lang.OutOfMemoryError主要原因有两个:

  1. Poor programming – infinite loop, not clearing memory by closing resources etc.

    编程不佳–无限循环,无法通过关闭资源等方式清除内存。
  2. Low Memory – java is running on less memory than required.

    内存不足– Java运行的内存少于所需的内存。

Java OutOfMemoryError –不良的编程示例 (Java OutOfMemoryError – Poor Programming Example)

Let’s look into a sample code that will throw java.lang.OutOfMemoryError: Java heap space because the program is going into infinite loop and objects are getting created but not getting garbage collected. So eventually JVM will run out of memory.

让我们看一个示例代码,该代码将引发java.lang.OutOfMemoryError: Java heap space因为该程序将进入无限循环,并且将创建对象但未收集垃圾。 因此,最终JVM将耗尽内存。

package com.journaldev.exceptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class JavaOutOfMemoryErrorExample {

	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();

		Random random = new Random();

		while (true)
			list.add(random.nextInt());

	}
}

When above code is executed, it will throw following exception after some time.

执行以上代码后,一段时间后将引发以下异常。

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.base/java.lang.Integer.valueOf(Integer.java:1050)
	at com.journaldev.exceptions.JavaOutOfMemoryErrorExample.main(JavaOutOfMemoryErrorExample.java:15)

This is an example of poor programming, but the good news is that the exception stack trace clearly points us to look for the code where this error occurred. However sometimes the problem might be in some other areas of the program, in that case we will need help of java profilers such as VisualVM to figure out where most of the memory is being allocated and how to optimize it.

这是一个编程不良的例子,但是好消息是,异常堆栈跟踪清楚地指示我们寻找发生此错误的代码。 但是有时问题可能出在程序的某些其他区域,在这种情况下,我们将需要Java分析器(例如VisualVM)的帮助来确定大部分内存在何处分配以及如何对其进行优化。

Java OutOfMemoryError –低内存示例 (Java OutOfMemoryError – Low Memory Example)

Let’s look at another example where the OutOfMemoryError is caused because we didn’t allocate enough memory for program to execute properly.

让我们看另一个OutOfMemoryError示例,因为我们没有为程序正确分配足够的内存。

public class JavaOutOfMemoryErrorExample {

	public static void main(String[] args) {
		
		Integer[] array = new Integer[1000*1000*100];
		System.out.println("Done");
		
	}

}

Let’s see what happens when we run above program with JVM max memory limit of 32 MB.

让我们看看当我们以JVM最大内存限制为32 MB运行上述程序时会发生什么。

$javac JavaOutOfMemoryErrorExample.java 
$java -Xmx32m JavaOutOfMemoryErrorExample
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at JavaOutOfMemoryErrorExample.main(JavaOutOfMemoryErrorExample.java:5)

As we can see that there is nothing wrong in the program, we are running it on very low memory. Let’s try to fix this OutOfMemoryError by increasing the JVM memory to 256MB and then 512 MB.

如我们所见,程序中没有错,我们在非常低的内存上运行它。 让我们尝试通过将JVM内存增加到256MB然后再增加512 MB来解决此OutOfMemoryError问题。

$java -Xmx256m JavaOutOfMemoryErrorExample
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at JavaOutOfMemoryErrorExample.main(JavaOutOfMemoryErrorExample.java:5)
$java -Xmx512m JavaOutOfMemoryErrorExample
Done
$

So it seems that our program runs fine when we provide enough memory to run it.

因此,当我们提供足够的内存来运行它时,我们的程序似乎运行良好。

Increasing the memory of JVM is a quick fix to solve the problem, unless you are running on very low memory. If you are already running on high JVM memory such as 2GB or more, then you should look into the application code to optimize it, look into thread dump and java profiler output to see why your application requires high memory and if you can reduce it.

除非您在非常低的内存上运行,否则增加JVM的内存是解决问题的快速解决方案。 如果您已经在高JVM内存(例如2GB或更高)上运行,则应查看应用程序代码以对其进行优化,并查看线程转储和Java Profiler输出,以了解您的应用程序为何需要较高的内存,以及是否可以减少内存。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/21010/java-lang-outofmemoryerror-java-heap-space

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值