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 spaceat 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 spaceat 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 spaceat 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

java.lang.OutOfMemoryError:Java堆空间相关推荐

  1. OOM系列之一:java.lang.OutOfMemoryError: Java堆空间问题详解

    第一篇:java.lang.OutOfMemoryError: Java heap space Java 应用程序只允许使用有限的内存量.此限制是在应用程序启动期间指定的.为了让事情变得更复杂,Jav ...

  2. 解码Java.Lang.OutOfMemoryError:PermGen空间

    Java开发人员最不了解的领域之一是垃圾收集. Java开发人员认为JVM负责垃圾收集,因此他们不必担心内存分配,释放等问题.但是,随着应用程序变得越来越复杂,垃圾收集也会变得越来越复杂,一旦性能变得 ...

  3. java内存溢出模拟_模拟实战排查堆内存溢出(java.lang.OutOfMemoryError: Java heap space)问题...

    前言: 模拟实战中排查堆内存溢出(java.lang.OutOfMemoryError: Java heap space)的问题. 堆内存溢出的原因:一般都是创建了大量的对象,这些对象一直被引用着,无 ...

  4. java.lang.OutOfMemoryError: Java heap space错误及...

    为什么80%的码农都做不了架构师?>>>    以下是从网上找到的关于堆空间溢出的错误解决办法: java.lang.OutOfMemoryError: Java heap spac ...

  5. java.lang.OutOfMemoryError: Java heap space的解决办法

    如果在启动过程中出现内存溢出问题,抛出类似如下异常信息: java.lang.OutOfMemoryError: Java heap space 可以尝试办法: A.修改Tomcat/bin/cata ...

  6. (转)java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)

    java.lang.OutOfMemoryError: Java heap space =================================================== 使用Ja ...

  7. Exception in thread main java.lang.OutOfMemoryError: Java heap space

    在做大批量数据测试的时候,服务端初始化出现问题 测试规模,没个站2万点量测数据,整个数据超过20万,初始化过程中对量测数据初始化,按站分别初始化,但是初始化到总数据量10万的时候就会报内存溢出错误. ...

  8. eclipse中报错:java.lang.OutOfMemoryError: Java heap space

    问题: 在eclipse中执行java程序.去重100多万的数据,报例如以下错误: java.lang.OutOfMemoryError: Java heap space 异常原因: 在JVM中假设9 ...

  9. java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)

    下面是从网上找到的关于堆空间溢出的错误解决的方法: java.lang.OutOfMemoryError: Java heap space ============================== ...

  10. java.lang.OutOfMemoryError: Java heap space 错误及解决办法

    java.lang.OutOfMemoryError: Java heap space =================================================== 使用Ja ...

最新文章

  1. 这本1900页的机器学习数学全书火了!完整版开放下载
  2. android调服务不更新,android – OnUpdate()不调用小部件服务
  3. 超全荧光定量PCR应用常见问题
  4. html中讲关键字加粗体,关键字(词)加粗 SEO
  5. 持续集成工具FinalBuilder使用心得
  6. 万字长文!Unix和Linux你不知道的那些历史(详解版)
  7. 机器学习--线性回归(LinearRegression)
  8. app个人健康管理系统开源_开源会促进心理健康吗?
  9. 三星Galaxy S22全系售价曝光:标准版起步或超5000元
  10. 黑域助手连接服务器才能用吗,自己装黑域一定要用电脑设置了才能用吗
  11. BSOD issue - collect complete memory dump
  12. python爬新闻并保存csv_Python简单爬虫导出CSV文件的实例讲解
  13. 谷歌浏览器Console不显示error信息
  14. Spelling Error Correction with Soft-Masked BERT
  15. 谷粒商城二十订单服务
  16. 计算机科学未来十年就业问题,未来10年“最有前途”的3个专业,就业率90%以上,多数人不看好...
  17. 让你的Onedrive网盘秒变网站,文件展示,直连下载,视频在线播放
  18. 江西余干:端午龙舟赛引冲突 官方否认有人被打死
  19. 卸载网易邮箱大师邮件从服务器删除,网易邮箱大师怎么删除邮箱 删除邮箱其实很简单...
  20. Lodash是如何实现深拷贝的

热门文章

  1. 32位ubuntu 使用pae
  2. jQuery入门[2]-选择器
  3. 排序1+3:基数排序(RadixSort),希尔排序(ShellSort)和快速排序(QuickSort)
  4. 文件共享服务器的搭建及调试——SAMBA
  5. [转载] numpy.inf
  6. [转载] Python的双端队列deque
  7. MVC文件上传07-使用客户端jQuery-File-Upload插件和服务端Backload组件裁剪上传图片...
  8. SpringBoot史前简述
  9. phoenix timestamp字段查询
  10. android中shape的属性