如何在Java中处理OutOfMemoryError? [英] How to handle OutOfMemoryError in Java?

查看:93
本文介绍了如何在Java中处理OutOfMemoryError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须序列化大约一百万个项目,当我运行我的代码时出现以下异常:

I have to serialize around a million items and I get the following exception when I run my code:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Unknown Source)
    at java.lang.String.<init>(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at org.girs.TopicParser.dump(TopicParser.java:23)
    at org.girs.TopicParser.main(TopicParser.java:59)

我该如何处理这个?

推荐答案

理想情况下,重构代码以减少内存使用。例如,也许您可​​以流式传输输出而不是将整个内容保存在内存中。

Ideally, restructure your code to use less memory. For example, perhaps you could stream the output instead of holding the whole thing in memory.

或者,只需使用为JVM提供更多内存 - Xmx 选项。

这篇关于如何在Java中处理OutOfMemoryError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆