user1371176
user1371176

Reputation: 1

Getting java.lang.OutOfMemoryError: Java heap space

I am getting an Exception in thread "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?? i am using all that in a Mac OS X 10.7.4

When i start HSQLDB, then i get by console this exception:

[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?

Upvotes: 0

Views: 7530

Answers (3)

Stephen C
Stephen C

Reputation: 718826

It is not 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.

It is not clear from the minimal error message you pasted into the question whether it is Tomcat or 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. 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.

  • 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; e.g. you could change your SQL queries so that less data needs to be pulled.

    • 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

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. The fact that the stacktrace is on the HSQLDB console confirms this to be the correct diagnosis. (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. 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.

I couldn't find a simple "how to" on increasing HSQLDB's heap size. It depends on whether you are using it as an embedded engine or launching HSQLDB in its own JVM. (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 (e.g. 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.

Upvotes: 2

Ali
Ali

Reputation: 12674

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

If you are developing in eclipse try increasing the permgen in eclipse.

If the error occurs when you are deployed on Tomcat, try increasing it in tomcat.

Upvotes: 0

ccleve
ccleve

Reputation: 15799

Could be either hsqldb or Tomcat. Unlikely to be Eclipse, because it spins off a separate JVM when it runs your app. 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.

Upvotes: 0

Related Questions