title: Spring Boot JPA Hibernate JVM heap is not released
body: <p>The following is a simplification of a more complicated setup. I describe this simple case to show the effect. </p>
  
  <p>I start Spring Boot and call a method. In this method all the contents of a MySQL database table is read via</p>
  
  <pre><code>Iterable&lt;myPojo&gt; myPojos = myPojoRepository.findAll();
  </code></pre>
  
  <p>Afterwards I leave this method.</p>
  
  <p>After finishing this method I get the message</p>
  
  <pre><code>Started Application in 70.893 seconds (JVM running for 72.899)
  </code></pre>
  
  <p>So Spring Boot is idling afterwards.</p>
  
  <p>But still the memory is not released.</p>
  
  <p><strong>How can I avoid that the JVM Heap is not released after the application is idling?</strong></p>
  
  <p>This is the result of VisualJM after the application is idling:</p>
  
  <pre><code>char[]                        1.080.623.712 (21.0%)  24.040.578 (23.4%)
  byte[]                        1.034.070.352 (20.1%)  17.280.824 (16.8%)
  java.lang.String                768.935.872 (14.9%)  24.029.246 (23.4%)
  java.lang.Object[]              556.181.104 (10.8%)   5.320.276 (5.1%)
  org.hibernate.engine.internal.MutableEntityEntry
                                   231.287.232 (4.5%)   2.628.264 (2.5%)
  org.hibernate.engine.spi.EntityKey
                                   224.752.040 (4.3%)   5.618.801 (5.4%)
  byte[][]                         212.407.904 (4.1%)   3.318.832 (3.2%)
  hello.web.model.MyPojo           185.852.968 (3.6%)   3.318.803 (3.2%)
  java.util.HashMap$Node           145.238.976 (2.8%)   3.025.812 (2.9%)
  com.mysql.jdbc.ByteArrayRow      132.752.120 (2.5%)   3.318.803 (3.2%)
  org.hibernate.engine.internal.EntityEntryContext$ManagedEntityImpl
                                   126.156.720 (2.4%)   2.628.265 (2.5%)
  hello.web.model.MyPojoCompoundKey
                                   120.376.680 (2.3%)   3.009.417 (2.9%)
  java.util.HashMap$Node[]         108.307.328 (2.1%)      16.558 (0.0%)
  java.lang.Float                   79.651.320 (1.5%)   3.318.805 (3.2%)
  int[]                             41.885.056 (0.8%)      54.511 (0.0%)
  java.util.LinkedHashMap$Entry     15.519.616 (0.3%)     242.494 (0.2%)
  java.io.File                      11.323.392 (0.2%)     235.904 (0.2%)
  org.springframework.boot.devtools.filewatch.FileSnapshot
                                    10.550.400 (0.2%)     219.800 (0.2%)
  java.lang.String[]                 8.018.808 (0.1%)      52.031 (0.0%)
  java.lang.reflect.Method           6.015.040 (0.1%)      37.594 (0.0%)
  java.io.File[]                     2.283.528 (0.0%)      16.746 (0.0%)
  </code></pre>
  
  <p>My effective-pom.xml shows that <code>hibernate 5.0.9.Final</code> is used.
  The table <code>my_pojo</code> contains 3.3 million entries.</p>
  
  <p>MyPojoRepository:</p>
  
  <pre><code>package hello.web.model;
  
  import com.querydsl.core.types.Predicate;
  import org.springframework.data.querydsl.QueryDslPredicateExecutor;
  import org.springframework.data.repository.PagingAndSortingRepository;
  
  import java.util.List;
  
  public interface MyPojoRepository
          extends PagingAndSortingRepository&lt;MyPojo, Long&gt;, 
                  QueryDslPredicateExecutor&lt;MyPojo&gt; {
  
      List&lt;MyPojo&gt; findAll(Predicate predicate);    
  }
  </code></pre>
  
  <p>MyPojo:
      package hello.web.model;</p>
  
  <pre><code>import lombok.AllArgsConstructor;
  import lombok.Builder;
  import lombok.Data;
  import org.apache.commons.beanutils.BeanComparator;
  import org.apache.commons.collections.comparators.ComparatorChain;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.IdClass;
  import java.io.Serializable;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  
  @Data
  @Entity
  @Builder
  @AllArgsConstructor
  @IdClass(MyPojoCompoundKey.class)
  public class MyPojo implements Serializable, Comparable&lt;MyPojo&gt; {
      public MyPojo() {    }
  
      @Id
      private String myId1;
      @Id
      private String myId2;
      @Id
      private String myId3;
      private Float myId4;
      private String myId5;
  
      @Override
      public int compareTo(MyPojo o) {
          return this.getMyId3().compareTo(o.getMyId3());
      }
      protected boolean canEqual(Object other) {
          return other instanceof MyPojo;
      }
      public static void sortByMyId1MyId3(List&lt;MyPojo&gt; myPojos) {
          ComparatorChain chain = new ComparatorChain(Arrays.asList(
                  new BeanComparator("myId1"),
                  new BeanComparator("myId3")
          ));
          Collections.sort(myPojos, chain);
      }
  }
  </code></pre>
  
  <p>myId1-3 and myId5 have a length of 10 characters in avarage. </p>
  
  <p>So again:
  <strong>How can I avoid that the JVM Heap is not released after the application is idling?</strong></p>