Solved

Hibernate connection memory leak problem

Posted on 2010-11-08
4
999 Views
Last Modified: 2012-05-10
Opening a session using a Hibernate SessionFactory is using lots of memory. Even when the session is closed the allocated memory doesn’t reduce. We observed, using the JProfiler/CPU tab view, that the buildSessionFactory used about 30% of the CPU time. This particular behavior is approximately similar in Hibernate 3.2.4, 3.3.1 and 3.5.4. We tested using the attached code.

Even this simple test used up HUGE memory.


public void useSession(String database)



throws Exception {



	Session session = null;



	try {



		Configuration myConfig = new  Configuration().setProperty(



					CONNECTION_DRIVER, SYBASE_DRIVER).setProperty(



					CONNECTION_DIALECT, SYBASE_DIALECT).setProperty(CONNECTION_URL,



					CommonConfiguration.get(database + CC_URL))



					.setProperty(CONNECTION_USERNAME,



									CommonConfiguration.get(CC_SYBASE_USER_ID))



					.setProperty(CONNECTION_PASSWORD,



									CommonConfiguration.get(CC_SYBASE_PASSWORD))



					.setProperty(CONNECTION_POOL_SIZE, ZERO).setProperty(



									CONNECTION_HOW_SQL, TRUE);







				SessionFactory factory = myConfig.buildSessionFactory();



				session = factory.openSession();



		}



		finally {



			if (session != null) {



					session.close();



			}



		}                               



	}

Open in new window

0
Comment
Question by:M_SYED
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Hegemon
ID: 34086363
Not clear what your question is. Yes, SessionFactory is a heavyweight and slow object and you normally create it only once. It consumes a lot of memory as it uses a number of libraries.

". Even when the session is closed the allocated memory doesn’t reduce. "

That's [almost] correct, SessionFactory should be kept in memory once built. However, Session objects are GC'd after closing and going out of scope.
0
 
LVL 2

Accepted Solution

by:
sandeep1984 earned 500 total points
ID: 34092331
You are creating session factory for every request which is wrong. This is a costly operation and need to be performed only once, and the same session factory should be used thereafter.

A sample util which will create session factory once. could be tweaked based on your needs.

http://www.koders.com/java/fid9CBB4F8729E37C58C6D34C7C5E0583BA9D0450D3.aspx?s=O#L3
0
 

Author Comment

by:M_SYED
ID: 34136898
Thank you for the comments.

We are in the process of reviewing and rewriting our code to ensure that SessionFactory is reused when possible.

I will keep this thred updated with the results.
0
 

Author Closing Comment

by:M_SYED
ID: 35408521
I had to do more research but the basic premise pointed out by the response was accurate.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now