简体   繁体   English

Java 在创建大量对象的 ArrayList 时出现堆空间错误?

[英]Java heap space error while creating ArrayList of large number of objects?

I have created an ArrayList (Java) of my custom class objects with a size around 3000. But when I run my code it gets the error "Heap space error".我已经创建了一个大小约为 3000 的自定义 class 对象的 ArrayList (Java)。但是当我运行我的代码时,它会收到错误“堆空间错误”。

I want to keep thousands of objects in an ArrayList at runtime without getting out of heap space.我想在运行时将数千个对象保留在 ArrayList 中,而不会超出堆空间。

How can the heap space error be avoided?如何避免堆空间错误?

It seems like you need to pass your program more memory.看来您需要更多地通过您的程序 memory。

Try running it like this:尝试像这样运行它:

java -Xmx256M MyApp

-Xmx sets the maximum heap size for Java. -Xmx设置 Java 的最大堆大小。 Putting M afterwards means megabytes, and G afterwards means gigabytes.后面加上 M 表示兆字节,后面加上 G 表示千兆字节。 So you can always do this if you have a bunch of memory:因此,如果您有一堆 memory,您总是可以这样做:

java -Xmx1g MyApp

Increase the Java heap size with the -Xmx Java system property.使用-Xmx Java 系统属性增加 Java 堆大小。 For example, give it as例如,给它作为

java -Xmx1024m Main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM