ciroanacleto / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HibernateMetadataUtil causes memory leak when dealing with many session factories

GoogleCodeExporter opened this issue · comments

When using the framework with many different database connections (aka session 
factories) the class HibernateMetadataUtil causes a memory leak because it puts 
every instance into a map (no fixed-capacity cache). This causes the garbage 
collector to move all session factory instances to the permanent generation 
instead of cleaning the instances after a session factory gets closed.
I would propose to use a LRU cache implementation instead of a hash map and 
also to introduce a public clearCache() method.

For example:

private static final int CACHE_CAPACITY = 128;
private static final float CACHE_HT_LOAD_FACTOR = 0.75f;
private static final boolean CACHE_HT_ACCESS_ORDER = true;

private static final Map<SessionFactory, HibernateMetadataUtil> map = new 
LinkedHashMap<SessionFactory, HibernateMetadataUtil>(CACHE_CAPACITY, 
CACHE_HT_LOAD_FACTOR, CACHE_HT_ACCESS_ORDER) {

    @Override
    protected boolean removeEldestEntry(final java.util.Map.Entry<SessionFactory, HibernateMetadataUtil> eldest) {

        return size() > CACHE_CAPACITY;
    }
};

public static void clearCache() {

    map.clear();
}

Original issue reported on code.google.com by hannes.s...@googlemail.com on 8 Nov 2011 at 3:56

Hannes,

I see what you are saying. This is certainly an easy change we could make.

I never envisioned a scenario where an application is connecting to so many 
different databases on the fly. I'm curious what your scenario is, if you don't 
mind sharing.

Also might it be better to use a WeakHashMap for this? What do you think?

Original comment by dwolvert on 10 Nov 2011 at 2:48

I'm importing a large amount of small-sectioned data files into SQLite 
databases in parallel and using your search framework for the subsequent 
transformation.

You're right. A WeakHashMap would be the most obvious choice and would also do 
the trick. It would be great if you don't mind doing this change. Due to the 
nature of SQLite I've anyway implemented a managing facility for the session 
factories which ensures that only one factory instance exists per database. It 
also holds the reference to the meta data utility to avoid multiple 
instantiations.

Original comment by hannes.s...@googlemail.com on 12 Nov 2011 at 7:06

WeakHashMap it is!

Original comment by dwolvert on 19 Nov 2011 at 12:18

  • Changed state: Accepted
  • Added labels: Milestone-Release1.1.1
Many thanks!

Original comment by hannes.s...@googlemail.com on 19 Nov 2011 at 12:29

Hashmap is a basically approach to hash data simply! the problem of caching 
it's very crucial and his clearing in strategic time instance during working 
the application.

many thanks about this wonderful project!! genericdao is a dream that transform 
in realty :)

Original comment by alessand...@gmail.com on 2 Dec 2011 at 6:44

Is the release date for version 1.1.0 already scheduled?

Original comment by hannes.s...@googlemail.com on 16 Dec 2011 at 7:58

Sadly, I have to admit that I won't be making the 1.1.1 release with this fix 
anytime soon. See the notice on the project home 
(http://code.google.com/p/hibernate-generic-dao/).

Original comment by dwolvert on 23 Dec 2011 at 2:23

Original comment by dwolvert on 15 Feb 2013 at 1:55

  • Added labels: Milestone-Release1.2.1
  • Removed labels: Milestone-Release1.1.1