title: Hibernate out of memory for Query 128MB
body: <p>I have one table with 3,244,977 Registers and 154.70 MB sized (data from phpmyadmin)</p>
  
  <p>I'm running a standalone java application and trying to load all this data through hibernate. My domain class is:</p>
  
  <pre><code>@Entity
  public class Register {
  
      @Id
      @Column(nullable = false, unique = true, updatable = false)
      private Long userId;
  
      private Date checked;
  
      @Column(nullable = false)
      private RegisterType tipo;
  
      private boolean preLiked = false;
      private boolean preCommented = false;
  
  }
  </code></pre>
  
  <p>Where RegisterType is an Enum which hibernate translates in an int.</p>
  
  <p>So as you can see my domain class is not that complex, considering that java will add some overhead to the data size stored in the database I setted my heap space to 4GB and I run my application with:</p>
  
  <pre><code>java -Xmx4G -cp '....classpath.....' com.tomatechines.bot.Starter
  </code></pre>
  
  <p>So even if the objects got 10 times bigger it should fit in the heap.</p>
  
  <p>But i'm getting  java.lang.OutOfMemoryError: Java heap space</p>
  
  <p>I was afraid that was other load together with this big amount of data and then i made a test... created a standalone jar that just try to load all data in that table without any other variable... but i'm still getting the exception.</p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
          at java.util.HashMap.resize(HashMap.java:703)
          at java.util.HashMap.putVal(HashMap.java:662)
          at java.util.HashMap.put(HashMap.java:611)
          at org.hibernate.internal.util.collections.IdentityMap.put(IdentityMap.java:94)
          at org.hibernate.engine.internal.StatefulPersistenceContext.addCollection(StatefulPersistenceContext.java:846)
          at org.hibernate.engine.internal.StatefulPersistenceContext.addUninitializedCollection(StatefulPersistenceContext.java:817)
          at org.hibernate.type.CollectionType.getCollection(CollectionType.java:739)
          at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:436)
          at org.hibernate.type.CollectionType.resolve(CollectionType.java:429)
          at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:151)
          at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)
          at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1132)
          at org.hibernate.loader.Loader.processResultSet(Loader.java:992)
          at org.hibernate.loader.Loader.doQuery(Loader.java:930)
          at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
          at org.hibernate.loader.Loader.doList(Loader.java:2610)
          at org.hibernate.loader.Loader.doList(Loader.java:2593)
          at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
          at org.hibernate.loader.Loader.list(Loader.java:2417)
          at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
          at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
          at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
          at com.tomatechines.utils.hibernate.GenericDAO.find(GenericDAO.java:183)
  </code></pre>
  
  <p>this same query runs on phpmyadmin in less than one second.</p>
  
  <p>Is it supposed to get that bigger when ran in java? hibernate makes the things grow 30 times the normal size in a database? how can i handle this without make the heap space bigger?</p>