morphia icon indicating copy to clipboard operation
morphia copied to clipboard

Memory Leak

Open evanchooly opened this issue 13 years ago • 12 comments

https://github.com/jmkgreen/morphia/issues/61

evanchooly avatar Jul 01 '13 15:07 evanchooly

For large data sets, you should over the cursor directly.

evanchooly avatar Jul 24 '13 14:07 evanchooly

Is this really the right answer? It seems like "don't use Morphia for large queries" puts a significant and undocumented limitation into the project. It appears that this behavior still exists and that MorphiaIterator is still being recommended for large queries. Is there any documentation on how to best use the raw cursor with Morphia?

ifellows avatar Mar 30 '16 17:03 ifellows

For large query results, yes, you should use the iterator so you're not pulling in every result in to memory at once.

evanchooly avatar Mar 30 '16 18:03 evanchooly

Just for my own clarity, are you suggesting using Query.fetch(), which yields a MorphiaIterator and the associated out.of.memory errors caused by the cache, or are you suggesting using the DBCursor iterator?

ifellows avatar Mar 30 '16 18:03 ifellows

Query.fetch(). I misspoke above when I said cursor instead of iterator.

evanchooly avatar Mar 30 '16 18:03 evanchooly

Perhaps I'm missing something as I am relatively new to Morphia, but is your statement "... so you're not pulling every result into memory at once" correct? Doesn't the EntityCache hold all of the results in memory? I'm getting the same out of memory errors reported in the original thread using fetch to run through a half million largish documents.

ifellows avatar Mar 30 '16 18:03 ifellows

It shouldn't be, no. That cache is supposed help with resolving references as I understand it. (Thought is design pre-dates my involvement.) If it's holding on to things when it shouldn't be then that's definitely a bug. I'll reopen and try build a test case to recreate.

evanchooly avatar Mar 30 '16 19:03 evanchooly

About two years ago, I ran into this issue and saw the "you should over the cursor directly" comment, and thought that there is no intention to fix this problem. So at that time, I worked around by manually extracting relevant information (DBCursor, Mapper, etc.) from the returned MorphiaIterator and doing my own mapping to the desired entity object. But since there is now intention to fix this issue, here is a small amount of analysis.

The source code for MorphiaIterator class reveals that it is using the same instance of EntityCache to map each DBObject retrieved from the DBCursor to the corresponding Morphia entity object. So I did a small test and concluded that the EntityCache is probably holding on to too many things.

Not wanting to set up a test database and actually inserting 4294967296 documents1, I wrote the following program instead:

public class Main {
    public static void main(String[] args) {
        final Morphia morphia = new Morphia();
        morphia.map(Foo.class);
        final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia-issue476");

        final Mapper mapper = morphia.getMapper();
        final EntityCache cache = mapper.createEntityCache();
        long id = 0L;
        for (int i = 0; i < 65536; i++) {
            System.out.println(i);
            for (int j = 0; j < 65536; j++) {
                id++;
                DBObject dbObject = new BasicDBObject("_id", id);
                Foo foo = mapper.fromDBObject(datastore, Foo.class, dbObject, cache);
            }
        }
    }
}

where the Foo class is:

@Entity
public class Foo {
    @Id
    public long id;
}

The program did not succeed. After printing "354", the program hung for a very long time and then printed "OutOfMemoryError: GC overhead limit exceeded".

However, if I add the following line after the call to mapper.fromDBObject():

cache.flush();

Then the program will successfully run to completion. However, I don't know whether clearing the cache after each mapping has any undesirable side effects.

Notes: 1 The number 4294967296 was chosen because ReferenceMap, as used in DefaultEntityCache, cannot hold this number of key-value entries.

yeefan avatar Apr 07 '16 20:04 yeefan

It turns out that in the DefaultEntityCache class, the existenceMap field will have a size of N when mapper.fromDBObject() is called N times with the same DefaultEntityCache instance, each call with a DBObject with a different _id field. So the existenceMap field keeps growing.

yeefan avatar Apr 07 '16 21:04 yeefan

I just ran into this issue (again) having a "OutOfMemoryError: GC overhead limit exceeded" using query.fetch(), what is the status of this? Is this ever going to be fixed or is there a recommended workaround for this?

matthiasamberg avatar Nov 14 '17 13:11 matthiasamberg

So I got same issue. I'm iterating with morphia cursor over a big collection (millions of entries) After couple of millions the app run out of heap space. I did head dump. screen shot 2018-03-21 at 10 14 41 screen shot 2018-03-21 at 10 14 53

One is with around 500k processed and the other one with 1500k. As you can see the DefaultEntityCache keeps feeling up.

The problem is exactly what yeefan pointed out.

We "fixed" this by wrapping iterator and calling fromDBObject with new DefaultEntityCache for each call.

Other "solution" is to call flush() after each call.

One cleaner solution is to implement EntityCache on our own. But IMO this should be supported by the morphia project already. So you should make DefaultEntityCache with option to limit the map size or even maybe also add second one with "no operation" EntityCache.

Steinkauz avatar Mar 21 '18 09:03 Steinkauz

It's a very large hammer but should work in the cases where it's needed. 2.0 will have a much finer grained control, I'm hoping.

evanchooly avatar Nov 25 '19 05:11 evanchooly