I have a terrible problem that hopefully has a very simple answer. I am running out of memory when I perform a basic
If I have code like this:
MyEntity myEntity;
for (Object id: someIdList) {
myEntity = find(id);
// do something basic with myEntity
}
And the find() method is a standard EntityManager related method:
public MyEntity find(Object id) {
return em.find(mycorp.ejb.entity.MyEntity.class, id);
}
This code worked a couple of weeks ago, and works fine if there are fewer items in the database. The resulting error I am facing is:
java.lang.OutOfMemoryError: GC overhead limit exceeded
The exception is coming from oracle toplink calling some oracle jdbc methods.
The loop exists because an EJBQL such as "select object(o) from MyEntity as o" will overload the application server when there are lots of records.
EntityManager
has most of time a short lifecycle (the common pattern is entitymanager-per-request). I suggest reading 5.1. Entity manager and transaction scopes. If you don't follow this pattern, then you'll have to deal with "memory handling". TheEntityManager
keeps entities in memory to track changes, that's how JPA works, you can't reasonably expect theEntityManager
to be able to load a whole database or to flush changes and detach entities for you. – Concourse