title: How can I prevent Eclipselink from consuming all memory with the cache?
body: <p>My application extracts large amounts of geometry data. I use Eclipselink 2.4.1 to persist that data in a MySQL database. The application works batch-style, i.e. I gather a set of data, then persist it, and continue with the next set, persist it and so on. Another application later reads that data and processes it, but this question is only about the first application that gathers the data.</p>
  
  <p>The data-gathering application runs for quite some time. I use a fixed set of <code>EntityManagers</code> which are created at startup and live until the application is finished. Extraction is multithreaded and there is one EntityManager per Thread. My problem is that no matter how I configure the cache, after a while EclipseLink eats up all the memory and after a while I get an <code>OutOfMemoryError</code>.</p>
  
  <p>I used <code>VisualVM</code> to extract heap dumps which I analyzed using <code>Eclipse Memory Analyzer</code>. A suspicious amount of memory is held by <code>EntityManagerImpl.extendedPersistenceContext.cloneMapping</code>. This map holds references to my geometry data objects. Over time, this map gets hundreds of megabytes in size and that's what causes the out of memory error.</p>
  
  <p>I have already tried the following:</p>
  
  <ul>
  <li>I use weak references by configuring <code>eclipselink.persistence-context.reference-mode=weak</code>. I have verified that <code>EntityManagerImpl.extendedPersistenceContext.cloneMapping</code> is of type <code>IdentityWeakHashMap</code>. From the <a href="http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Shared_and_Isolated#Weak_Reference_Mode" rel="nofollow">documentation about weak caches</a> I would expect that using reference mode <code>weak</code> solves the problem. Unfortunately, the garbage collector still does not claim the entries and I keep getting out of memory errors.</li>
  <li>I tried to turn off caching completely with <code>eclipselink.cache.shared.default=false</code>. The problem persists.</li>
  </ul>
  
  <p>Does anybody have a suggestion as to what's going on here and how I could solve that problem? I'm also open to suggestions how to circumvent the problem.</p>