Hibernate - java.lang.OutOfMemoryError: Java heap space

前端 未结 3 602
别那么骄傲
别那么骄傲 2021-01-27 09:17

I get this exception:

Exception in thread \"AWT-EventQueue-0\" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    a         


        
相关标签:
3条回答
  • 2021-01-27 09:37

    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.

    0 讨论(0)
  • 2021-01-27 09:46

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

    0 讨论(0)
  • 2021-01-27 09:49

    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.

    0 讨论(0)
提交回复
热议问题