title: Spring + Hibernate: Query Plan Cache Memory usage
body: <p>I'm programming an application with the latest version of Spring Boot. I recently became problems with  growing heap, that can not be garbage collected. The analysis of the heap with Eclipse MAT showed that, within one hour of running the application, the heap grew to 630MB and with Hibernate's SessionFactoryImpl using more than 75% of the whole heap.</p>
  
  <p><img src="https://i.stack.imgur.com/fSsUr.png" alt="enter image description here"></p>
  
  <p>Is was looking for possible sources around the Query Plan Cache, but the only thing I found was <a href="https://stackoverflow.com/questions/24359088/high-memory-usage-when-using-hibernate">this</a>, but that did not play out. The properties were set like this:</p>
  
  <pre><code>spring.jpa.properties.hibernate.query.plan_cache_max_soft_references=1024
  spring.jpa.properties.hibernate.query.plan_cache_max_strong_references=64
  </code></pre>
  
  <p>The database queries are all generated by the Spring's Query magic, using repository interfaces like <a href="http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation" rel="noreferrer">in this documentation</a>. There are about 20 different queries generated with this technique. No other native SQL or HQL are used.
  Sample:</p>
  
  <pre><code>@Transactional
  public interface TrendingTopicRepository extends JpaRepository&lt;TrendingTopic, Integer&gt; {
      List&lt;TrendingTopic&gt; findByNameAndSource(String name, String source);
      List&lt;TrendingTopic&gt; findByDateBetween(Date dateStart, Date dateEnd);
      Long countByDateBetweenAndName(Date dateStart, Date dateEnd, String name);
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>List&lt;SomeObject&gt; findByNameAndUrlIn(String name, Collection&lt;String&gt; urls);
  </code></pre>
  
  <p>as example for IN usage.</p>
  
  <p>Question is: Why does the query plan cache keep growing (it does not stop, it ends in a full heap) and how to prevent this? Did anyone encounter a similar problem?</p>
  
  <p>Versions:</p>
  
  <ul>
  <li>Spring Boot 1.2.5</li>
  <li>Hibernate 4.3.10</li>
  </ul>