简体   繁体   English

获取java.lang.OutOfMemoryError:Java堆空间

[英]Getting java.lang.OutOfMemoryError: Java heap space

I am getting an Exception in thread "HSQLDB Connection @3c50507" 我在线程“HSQLDB Connection @ 3c50507”中遇到异常

java.lang.OutOfMemoryError: Java heap space, when running a JSP.

what is the thing that is out of memory? 什么是内存不足? eclipse, HSQLDB or Tomcat?? eclipse,HSQLDB或Tomcat ?? i am using all that in a Mac OS X 10.7.4 我在Mac OS X 10.7.4中使用了所有这些

When i start HSQLDB, then i get by console this exception: 当我启动HSQLDB时,我通过控制台获得此异常:

[Server@122ce908]: From command line, use [Ctrl]+[C] to abort abruptly
Exception in thread "HSQLDB Connection @2e716cb7" java.lang.OutOfMemoryError: Java heap space
    at org.hsqldb.lib.HsqlByteArrayOutputStream.ensureRoom(Unknown Source)
    at org.hsqldb.rowio.RowOutputBinary.ensureRoom(Unknown Source)
    at org.hsqldb.lib.HsqlByteArrayOutputStream.write(Unknown Source)
    at org.hsqldb.rowio.RowOutputBinary.writeByteArray(Unknown Source)
    at org.hsqldb.rowio.RowOutputBinary.writeBinary(Unknown Source)
    at org.hsqldb.rowio.RowOutputBase.writeData(Unknown Source)
    at org.hsqldb.Result.write(Unknown Source)
    at org.hsqldb.Result.write(Unknown Source)
    at org.hsqldb.ServerConnection.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)

What does this all mean? 这是什么意思呢?

It is not Eclipse. 它不是Eclipse。 Your "application" is not running in the same JVM as Eclipse, and the minimal error message you posted makes it clear that this is happening in the application ... in the broad sense. 您的“应用程序”未在与Eclipse相同的JVM中运行,并且您发布的最小错误消息清楚地表明,这在应用程序中发生......广义上讲。

It is not clear from the minimal error message you pasted into the question whether it is Tomcat or HSQLDB. 从您粘贴到问题的最小错误消息中不清楚它是Tomcat还是HSQLDB。 I think it is more likely to be Tomcat ... and that the problem is occurring while you are pulling a large result-set from the database. 我认为它更可能是Tomcat ......并且当你从数据库中提取大量结果集时,问题就出现了。 However, without a full stacktrace, I'm just guessing. 但是,如果没有完整的堆栈跟踪,我只是在猜测。

If it is what I suspect, then you've got two options: 如果这是我怀疑的,那么你有两个选择:

  • You can increase the JVM heap size for the JVM that is running Tomcat. 您可以增加运行Tomcat的JVM的JVM堆大小。

  • You can figure out what has triggered your application to fill up the heap. 您可以找出触发应用程序填充堆的内容。

    • It could simply be that your application design requires you to pull a large amount of data from the database and hold it in memory. 可能只是您的应用程序设计要求您从数据库中提取大量数据并将其保存在内存中。 In that case, you've no choice but to either increase the heap size, or (somehow) enforce some limit on the "problem size". 在这种情况下,你别无选择,只能增加堆大小,或者(不知何故)对“问题大小”强制实施一些限制。

    • It could be that your application doesn't really need to pull all of that data; 可能是您的应用程序并不真正需要提取所有数据; eg you could change your SQL queries so that less data needs to be pulled. 例如,您可以更改SQL查询,以便需要提取更少的数据。

    • It could be that you don't need to hold all of the data in memory at the same time. 可能是您不需要同时将所有数据保存在内存中。

    • It could be that you've got an underlying memory leak in your application, and THAT is what is using up all of the memory. 可能是您的应用程序中存在潜在的内存泄漏,这就是耗尽所有内存的原因。 In that case, increasing the heap size is like sticking a bandaid on a car crash victim. 在这种情况下,增加堆大小就像在车祸受害者身上贴上一个绑带。 The patient is going to die unless you fix the real problem. 除非你解决了真正的问题,病人才会死。


UPDATE based on the belated stacktrace UPDATE基于迟来的堆栈跟踪

If the problem is occurring you are fetching an image from the database, then that exception is coming from the database side. 如果问题出现在您从数据库中提取图像,那么该异常来自数据库端。 The method names say that it is WRITING. 方法名称表示它是WRITING。 The fact that the stacktrace is on the HSQLDB console confirms this to be the correct diagnosis. 堆栈跟踪在HSQLDB控制台上的事实证实这是正确的诊断。 (A memory leak is unlikely at this point. You just need to increase the heap size ... or refrain from storing huge images in your database!) (此时不太可能发生内存泄漏。您只需要增加堆大小......或者不要在数据库中存储大量图像!)

On the other hand, if it is occurring while you are storing an image to the database, then it is on the Tomcat side. 另一方面,如果在将图像存储到数据库时发生,则它位于Tomcat端。 One of the other answers has a link on how to deal with that, 其中一个答案有关于如何处理它的链接,

Either way, storing large images in a database is not good from an efficiency standpoint, and its likely to stress your database / webserver infrastructure if you do so ... just like you are seeing. 无论哪种方式,从效率的角度来看,将大型图像存储在数据库中并不好,如果您这样做,它可能会给您的数据库/网络服务器基础设施带来压力......就像您所看到的那样。


UPDATE on increasing HSQLDB's stack size. UPDATE上增加HSQLDB的堆栈大小。

I couldn't find a simple "how to" on increasing HSQLDB's heap size. 我无法找到增加HSQLDB堆大小的简单“如何”。 It depends on whether you are using it as an embedded engine or launching HSQLDB in its own JVM. 这取决于您是将其用作嵌入式引擎还是在其自己的JVM中启动HSQLDB。 (My guess is that you are doing the latter.) In the former case, you handle the problem by increasing the heap size for the application that embeds it (eg Tomcat). (我的猜测是你正在做后者。)在前一种情况下,你通过增加嵌入它的应用程序的堆大小来处理问题(例如Tomcat)。 Otherwise, if you are launching from the command line, add the -Xmx and -Xms parameters to the java command line ... as described in the java manual page. 否则,如果从命令行启动,请将-Xmx和-Xms参数添加到java命令行...,如java手册页中所述。

Could be either hsqldb or Tomcat. 可以是hsqldb或Tomcat。 Unlikely to be Eclipse, because it spins off a separate JVM when it runs your app. 不太可能是Eclipse,因为它在运行你的应用程序时会旋转一个单独的JVM。 The right way to debug it is to look at the stack trace to see if it gives a clue. 调试它的正确方法是查看堆栈跟踪以查看它是否给出了线索​​。 If not, use a profiler. 如果没有,请使用分析器。

The error usually occurs when you create too many objects on the heap, I suspect you may be creating too many Strings when reading from the DB. 当您在堆上创建太多对象时,通常会发生错误,我怀疑从DB读取时可能会创建太多Strings Either way, the problem is related to the VM and one solution (if you don't feel your code could be better) would be to increase the permgen of the JVM. 无论哪种方式,问题都与VM和一个解决方案(如果你觉得你的代码不是更好)有关,那就是增加JVM的permgen。

If you are developing in eclipse try increasing the permgen in eclipse . 如果你正在eclipse中开发,请尝试在eclipse中增加permgen

If the error occurs when you are deployed on Tomcat, try increasing it in tomcat . 如果在Tomcat上部署时发生错误,请尝试在tomcat中增加它

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

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