-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to overcome OutOfMemoryError exception?
PostPosted: Mon Dec 28, 2009 5:56 pm 
Newbie

Joined: Mon Dec 28, 2009 5:34 pm
Posts: 2
My Java program which uses Hibernate is falling over on me with an OutOfMemoryError exception. I'd appreciate any advice on how to reduce the memory usage. Do I need to disable some caching that Hibernate does by default? Do I need to reduce some cache size?

Some Background

I have a problem with a Java program that I have written and I was hoping to use Hibernate to help me solve it. My Java program generates loads of objects and consumes large amounts of memory. So I was hoping to use Hibernate to store the objects in a database and then just retrieve them when I need them.


Test Program

So to test this, I decided to create a large number of objects and store them to the database using Hibernate to ensure that the memory usage did not rise. Each time I would create an object I would save it to the database and then dereference the object straight away. Below is my little bit of code. When I run this code it falls over with an java.lang.OutOfMemoryError exception.

I decided then to profile the code and it seems the large amounts of memory are being used up in instances of these two classes

org.hibernate.engine.EntityEntry
org.hibernate.engine.EntityKey

So, pretty much a 1-to-1 mapping for each object I save to the database. I presume that Hiberate is holding on to the reference in some cache? But how do I clear this so that my memory usage stays constant regardless of how many objects I create (i.e. stays constant after a certain startup period)?


My Code

Code:
       
---------------------------------------------------------
Main Java Class
---------------------------------------------------------
        SessionFactory sessions = new Configuration().configure().buildSessionFactory();
        Session session = sessions.openSession();
        try {
            for (int i = 1; i <= 500000; i++){
                Person p1 = new Person();
                p1.setName("Person" + i);
                session.save(p1);               
                p1 = null;
                if (i % 1000 == 0){
                   System.out.println(i + " objects");
                   session.flush();
                }
            }
        } catch ( HibernateException e ) {
            e.printStackTrace();
        } finally {
            session.close();
        }

----------------------------------------------------------------
My Entity Java Class
---------------------------------------------------------
import java.io.Serializable;

public class Person implements Serializable {
   
    private int id;
    private String name;
   
    protected Person() {
    }
   
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
   
    public int getId() {
        return id;
    }
   
    public void setId(int id) {
        this.id = id;
    }
   
    public String getName() {
        return name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
   
}

---------------------------------------------------------
hibernate.cfg.xml
---------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="connection.url">jdbc:derby://localhost:1527/myDB</property>
        <property name="connection.username">me</property>
        <property name="connection.password">mine</property>
       
        <property name="connection.autocommit">true</property>
       

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <!-- Mapping files -->
        <mapping resource="Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: How to overcome OutOfMemoryError exception?
PostPosted: Mon Dec 28, 2009 6:54 pm 
Newbie

Joined: Mon Dec 28, 2009 5:34 pm
Posts: 2
I found some information on the second level cache in Hibernate. So, adding the lines in bold below seemed to help - it disables the use of the second level cache. I was able to add the 500,000 records to my table without the program falling over with an OutOfMemoryError exception. I'm not sure if this is the best way to go about it, any advice?

The runtime performance seems to get quite slow. I haven't done any analysis on it yet to see how slower it gets over time. But any advice on what I can do to speed it up?


SessionFactory sessions = new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
session.setCacheMode(CacheMode.IGNORE);

try {
for (int i = 1; i <= 500000; i++){
Person p1 = new Person();
p1.setName("Sang Shin" + i);
session.save(p1);
p1 = null;
if (i % 1000 == 0){
System.out.println(i + " objects");
session.flush();
session.clear();
}
}
} catch ( HibernateException e ) {
e.printStackTrace();
} finally {
session.close();
}


Top
 Profile  
 
 Post subject: Re: How to overcome OutOfMemoryError exception?
PostPosted: Thu Apr 08, 2010 4:59 pm 
Newbie

Joined: Thu Apr 08, 2010 4:54 pm
Posts: 2
Even I have the similar Issue where in the analyzer show me

-java.util.LinkedHashMap$Entry
-org.hibernate.engine.EntityEntry is taking 506,222 objects which is consuming 70% of memory.

Were you able to find the solution for the problem you had.

It would be great if you let us know if you have any solutions.

Thanks,
Naveen


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.