Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
JPA / Hibernate Out of Memory Exception

I've recently moved a project that was using native Hibernate entities and session management over to Spring, with annotation driven dependency injection and transaction management.

I have an Entity structure like this:

@Entity
@Table(name = "PARENT",uniqueConstraints=@UniqueConstraint(columnNames={"parentName"}))
public class Parent implements Serializable, {
    static final long serialVersionUID = 1L;

    @Id
    private int id;


    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Child> child = new HashSet<Child>();
}



@Entity
@Table(name = "CHILD", uniqueConstraints = @UniqueConstraint(columnNames = {
        "childName", "parent_id" }))
@SequenceGenerator(name = "CHILD_SEQ", allocationSize = 1, sequenceName = "CHILD_SEQ")
public class Child implements Serializable {

    static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHILD_SEQ")
    private int id;
    private String childName = ""; //$NON-NLS-1$

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Parent parent;
}

Here's what happens.

  • I get all the parent objects which contain individual lists of child objects. This works quickly, I have the full object tree in memory in my debugger I can see every Child object is loaded correctly.

    public List<Parent> getParents() {
        return em.createQuery("from Parent",Parent.class).getResultList();
    }
    
  • I then try and get every Child object and the application freezes my PC slows down and after a few hours I get the Out on memory exception.

    public List<Child> getChildren() {
        return em.createQuery("from Child",Child.class).getResultList();
    }
    

I have looked at the generated SQL and for the first method it seems to logically break down the calls in to individual sets of objects and for the seconds it seems to construct a monster sized query that I can't really follow and that query doesn't appear to be returning, although I can't really tell.

What I cannot understand is why the query to the parent works so quickly and gives me every single object but the child query just breaks.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When you start with the parents Hibernate can read all parents (simple) and join with the child table (again, simple). Each child has exactly one parent, which is the one we join with, so that is enough. While the entire database is read into memory that is no problem as it is small.

When you start with the children on the other hand Hibernate must first fetch the parent for each child. That means one join with the parent table. However, those parents have children, so we must join with the child table again in order to find them.

Your classes are not complete, parent name is missing and you mention other collections. If the other collections are eager as well they would have to be fetched with joins for the first child level and the second child level as well, adding two more joins per collection. That makes for a very complex query!

over 4 years ago · Santiago Trujillo Report

0

It might be a memory leak causing this. You can increase your heap space using.

-Xms<size> set initial Java heap size

-Xmx<size> set maximum Java heap size

http://javarevisited.blogspot.com.ng/2011/09/javalangoutofmemoryerror-permgen-space.html

However, It's good to know what could be the cause of java.lang.OutOfMemoryError: Java heap space. Using a profiler can help you figure out what's eating the heap size. If you are using Netbeans, you can use https://profiler.netbeans.org/ or find profiler compatible with your IDE.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!