0

I get this exception:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at java.util.AbstractMap.toString(AbstractMap.java:493)
    at org.hibernate.pretty.Printer.toString(Printer.java:59)
    at org.hibernate.pretty.Printer.toString(Printer.java:90)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:97)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)

At this code:

Query query = null;
        Transaction tx= session.beginTransaction();
        if (allRadio.isSelected()) {
            query = session.createQuery("select d from Document as d, d.msg as m, m.senderreceivers as s where m.isDraft=0 and d.isMain=1 and s.organization.shortName like '" + search + "' and s.role=0");
        } else if (periodRadio.isSelected()) {
            query = session.createQuery("select d from Document as d, d.msg as m, m.senderreceivers as s where m.isDraft=0 and d.isMain=1 and s.organization.shortName like '" + search + "' and s.role=0 and m.receivingDate between :start and :end");
            query.setParameter("start", start);
            query.setParameter("end", end);
        }
        final List<Document> documents = query.list();


        query = session.createQuery("select o from Organization as o");
        List<Organization> organizations = query.list(); <---AT THIS LINE
        tx.commit();

Im making 2 consecutive queries. If i comment out 1 of them the other works fine.

if i remove transaction thingy exception dissappears. What's going on? Is this a memory leak or something? Thanks in advance.

1
  • Is your list organizations large? Commented Dec 13, 2011 at 10:08

3 Answers 3

8

A tip I picked up from many years of pain with this sort of thing: the answer is usually carefully hidden somewhere in first 10 lines of the stack trace. Always read the stack trace several times, and if that doesn't give enough help, read the source code of the methods where the failure happens.

In this case the problem comes from somewhere in Hibernate's pretty printer. This is a logging feature, so the problem is that Hibernate is trying to log some enormous string. Notice how it fails while trying to increase the size of a StringBuilder.

Why is it trying to log an enormous string? I can't say from the information you've given, but I'm guessing you have something very big in your Organization entity (maybe a BLOB?) and Hibernate is trying to log the objects that the query has pulled out of the database. It may also be a mistake in the mapping, whereby eager fetching pulls in many dependent objects - e.g. a child collection that loads the entire table due to a wrong foreign-key definition.

If it's a mistake in the mapping, fixing the mapping will solve the problem. Otherwise, your best bet is probably to turn off this particular logging feature. There's an existing question on a very similar problem, with some useful suggestions in the answers.

Sign up to request clarification or add additional context in comments.

1 Comment

this is the correct answer, the printer just goes down the structure till it loads the entire database. (i bet organization has some collections within)
2

While such an error might be an indicator for a memory leak, it could also just result from high memory usage in your program.

You could try to amend it by adding the following parameter to your command line (which will increase the maximum heap size; adapt the 512m according to your needs):

java -Xmx512m yourprog

If it goes away that way, your program probably just needed more than the default size (which depends on the platform); if it comes again (probably a little later in time), you have a memory leak somewhere.

5 Comments

I dont have so much memory. I changed to 256m still same thing. This code worked fine for some time, i might changed something accidently. How do i track down this memory leak?
Hopefully you do have some kind of version control (git, svn, ...)? The basic steps are easy: Try old versions of the software on the same data; compare the first one which doesn't run anymore with the last version which still does run without the exception ;). In practice it might not be so easy since a lot of other things probably have changed as well
Im just started learning programming, its a project on my pc. No older versions.
If you don't yet do so, I really recommend using a version control system, even when developing only locally! git or mercurial are suited for that very well, since you don't need anything else apart from your local working copy. Be sure to familiarize yourself a little with the system's possibilities, and check in often! But we're straying a little from the original topic :). Without a version control system, however, its really hard to track accidental changes, there's no definite starting point I can think of for that...
my project is really small. Im making 2 consecutive queries. If i comment out 1 of them the other works fine.
0

You need to increase the JVM heap size. Start it with -Xmx256m command-line param.

1 Comment

unless you expect the dude to fit the entire database in memory the solution wont cut

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.