The moose likes Object Relational Mapping and the fly likes Possible memory leak during recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Possible memory leak during recursion" Watch "Possible memory leak during recursion" New topic

Possible memory leak during recursion

Thai C. Tran

Joined: Jul 05, 2009
Posts: 26
Hi all,

I have recursion function, which access to the database through hibernate 3 in order to count all the members of a directory. Here is the code of the function

And here is the code for DirectoryEntity implementation

And for the findbyId function

everytime the function is finished, the memory is increased significantly and objects in the heap is not garbage collected. I tried to use MAT to use analyse the problem (please see the attachement). It looks like I have a issue with opening/closing transation but still have no idea how to fix it. Can anyone have any suggestion, please? Please ask more code if necessary

[Thumbnail for MAT.PNG]

Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 187
If you are indeed not closing transactions, then you didn't post enough information. How is hibernate setup? What is the reference to "dao" in this line: "dao.getSession().get(ReflectionUtils.getSuperClassGenricType(getClass()), id);" pointing to? What "dao"? How does that "dao" implement "getSession()"?
Thai C. Tran

Joined: Jul 05, 2009
Posts: 26
Sorry, I didn't make the question clear. As the title said, it is possible to leak; but actually, it is not . I have checked all the connection handling and cannot find anything wrong with it. After using yourkit tool (it allows to check the heap space continuously with the force GC running function), it turned out that because we are using thread in order to run that function, the heap space keeps increasing so fast but the GC cannot run fast enough to free the unused memory. So, what I did is trying to clear the session when possible (after each thread) and the most important thing is using this suggestion. The test case passed even with the 64 MB.

Thank for your time
I agree. Here's the link:
subject: Possible memory leak during recursion
It's not a secret anymore!