-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Hibernate associations using too much memory
PostPosted: Tue Feb 23, 2016 1:15 pm 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
I have a table "class" which is linked to tables "student" and "teachers". A "class" is linked to multiple students and teachers via foriegn key relationship.

When I use hibernate associations and fetch large number of entities(tried for 5000) i am seeing that it is taking ~4 times more memory than if i just use foreign key place holders. Is there something wrong in hibernate association? Any way to improve this?


This is how the schema is:

Code:
class(id,className)
student(id,studentName,class_id)
teacher(id,teacherName,class_id)

class_id is foreign key..


Case #1 - Hibernate Associations
1)in Class Entity , mapped students and teachers as :

Code:
@Entity
@Table(name="class")
public class Class {

private Integer id;
private String className;

private Set<Student> students = new HashSet<Student>();
private Set<Teacher> teachers = new HashSet<Teacher>();

@OneToMany(fetch = FetchType.EAGER, mappedBy = "classRef")
@Cascade({ CascadeType.ALL })
@Fetch(FetchMode.SELECT)
@BatchSize(size=500)
public Set<Student> getStudents() {
    return students;
}



2)in students and teachers , mapped class as:
Code:
@Entity
@Table(name="student")
public class Student {

private Integer id;
private String studentName;
private Class classRef;

@ManyToOne
@JoinColumn(name = "class_id")
public Class getClassRef() {
    return classRef;
}


Query used :
Code:
sessionFactory.openSession().createQuery("from Class where id<5000");


This however was taking a Huge amount of memory.



Case #2- Remove associations and fetch seperately

1)No Mapping in class entity
Code:
@Entity
@Table(name="class")
public class Class {

private Integer id;
private String className;


2)Only a placeholder for Foreign key in student, teachers
Code:
@Entity
@Table(name="student")
public class Student {

private Integer id;
private String studentName;
private Integer class_id;


Queries used :
Code:
sessionFactory.openSession().createQuery("from Class where id<5000");
sessionFactory.openSession().createQuery("from Student where class_id = :classId");
sessionFactory.openSession().createQuery("from Teacher where class_id = :classId");


Note - Shown only imp. part of the code. I am measuring memory usage of the fetched entities via JAMM library.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 5:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You should replace the collection with a query which you can paginate.

This way you don't have to fetch 5000 child records, which you might not even need in the UI anyway.
With a query, you can even use a projection so that you can fine-tune which child entity properties are selected.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 9:09 am 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
This is used for a background rule evaluation process. So, i do need to fetch all the data.
The problem is that hibernate associations approach is taking too much memory. It ultimately has pretty much the same data that is there in second approach but the memory usages is Very large.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 9:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's why I suggested to remove the EAGER one-to-many association and use a query instead.
The HQL/JPQL query can select a limited number of child records and you won't have any memory issue.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 10:04 am 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
Maybe i was not clear. Although i am using EAGER but even in the alternative approach using foreign key i fetch exactly number of records for all the entities

Take a look at the queries in Case #2.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 10:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
But you still fetch 5000 entities even with a query. Try to use pagination and process those entries in batches.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 1:07 pm 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
Just reiterating the problem...guess there's a difference in understanding...

Case #1 fetch entity class with batch size of 500..where class is associated to student and teachers via hibernate associations as above..

Case#2 fetch classes, then fetch students and teachers using id's of fetched classes...NO associations And NO batching..

Then we create wrapper entity around a class, it's students and teachers..


Case#2 takes 60-70% less memory..hibernate association seem to be going fishy..


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's strange as I don't see how the Collection object should take that much memory. You need to profile it and see what's causing so much memory.


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Feb 24, 2016 11:05 pm 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
Will try that and post my findings.. Wanted to check if it's a known or an obvious issue...


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed Mar 23, 2016 2:34 am 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
Can this be related to Hibernate maintaining unmodified snapshots for dirty checking and all? I tried setting the query to readOnly but did not help : http://stackoverflow.com/questions/3532 ... uch-memory


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Wed May 04, 2016 6:39 am 
Newbie

Joined: Tue Feb 23, 2016 1:04 pm
Posts: 11
Hi mihalcea_vlad , do you have any suggestions on how to fix the above problem based on the profiling results above?


Top
 Profile  
 
 Post subject: Re: Hibernate associations using too much memory
PostPosted: Mon May 16, 2016 10:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The only suggestion I have is to replace the collection with a query which you can paginate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.