ztx1491 / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao
1 stars 0 forks source link

HibernateMetadataUtil causes memory leak when dealing with many session factories #93

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
WeakHashMap it is!

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

GoogleCodeExporter commented 8 years ago
Many thanks!

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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago

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