Code:
public static void main(String[] args) {
// memory close to -Xmx value
List<EntityManagerFactory> emfs = IntStream.range(0, 50).mapToObj(
n -> Persistence.createEntityManagerFactory("store",
ImmutableMap.of(
"javax.persistence.jdbc.driver", "net.sourceforge.jtds.jdbc.Driver",
"javax.persistence.jdbc.url", "jdbc:jtds:sqlserver://xxx:1433/xxx",
"javax.persistence.jdbc.user", "xxx",
"javax.persistence.jdbc.password", "xxx"
))
).collect(toList());
// ~1gib of memory.
emfs.forEach(EntityManagerFactory::close);
try { Thread.sleep(60000); } catch (InterruptedException e) {}
// *STILL* ~1gib of memory.
}
Some notes:
- persistence.xml is stored in a jar that is rather large (20mib), I have artificially increased size to make the problem surface more easy.
- if run exploded (no jar) then issue does not exist
- the memory usage quoted above is the process memory.
Current theory is that hibernate is unpacking the jar each time it creates an EntityManagerFactory and holding onto the contents to create a memory leak.
At this stage only option is to test if other ORM has same issue.