简体   繁体   English

如何更正“Java 堆空间 - 内存不足”错误

[英]How can I correct a “Java heap space - Out of Memory” error

I'm currently running my system against a rather large dataset and am getting the error.我目前正在针对相当大的数据集运行我的系统,但出现错误。 'Out of memory. '内存不足。 Java Heap Space'. Java堆空间'。

Is there anyway to get around this or is it just a case of the dataset is too large and can't be used?有没有办法解决这个问题,还是只是数据集太大而无法使用的情况?

In general, you can either一般来说,你可以

  • give it more memory eg increase the maximum heap size, but don't give it more than about 90% of main memory.给它更多的内存,例如增加最大堆大小,但不要给它超过大约 90% 的主内存。 BTW the default is 25% of main memory up to 32GB.顺便说一句,默认值是主内存的 25%,最高可达 32GB。
  • optimise the code so that it uses less memory, eg use a memory profiler.优化代码,使其使用更少的内存,例如使用内存分析器。 You can use a more efficient data structure or load portions of data into memory at a time.您可以使用更高效的数据结构或一次将部分数据加载到内存中。
  • break up the data so it own works on a portion at a time.分解数据,使其一次只处理一部分。

If it's not the dataset that's eating up memory, it could be that you are not freeing up objects once they are inactive.如果不是数据集占用了内存,则可能是因为您没有在对象处于非活动状态时释放它们。

This is typically due to keeping references to very large objects or to lots objects laying around long after they are no longer needed.这通常是由于保留对非常大的对象的引用,或者在不再需要它们之后很长一段时间内仍然存在大量对象。 This is most likely references that are static variables, but it can also be references to large temporary variables (eg, large StringBuilder objects) within methods that are still active.这很可能是静态变量的引用,但它也可能是对仍处于活动状态的方法中的大型临时变量(例如,大型StringBuilder对象)的引用。

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

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