title: Memory leak , tomcat 7
body: <p>When you start tomcat,
  it consumes 70mbs of memory,
  when I make requests then it consumes 80mbs,
  never back to 70mbs</p>
  
  <p>Crud code</p>
  
  <pre><code>package br.com.controlevendas.recurso;
  
  
  import java.io.IOException;
  import java.util.List;
  
  import javax.ws.rs.Consumes;
  import javax.ws.rs.DELETE;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.PUT;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import br.com.controlevendas.controle.ClienteControle;
  import br.com.controlevendas.modelo.Cliente;
  
  @Component
  @Path("/cliente")
  public class ClienteRecurso {
  
      @Autowired
      private ClienteControle clienteControle;
  
      @GET
      @Path("/lista")
      @Produces(MediaType.APPLICATION_JSON)
      public List&lt;Cliente&gt; listar() {
  
  
          List&lt;Cliente&gt; clienteList = this.clienteControle.listar();
  
          try {
              this.clienteControle.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return clienteList;
      }
  
      @POST
      @Path("")
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public Cliente inserir(Cliente cliente) {
  
          cliente = this.clienteControle.salvar(cliente);
  
          try {
              this.clienteControle.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return cliente;
      }
  
      @PUT
      @Path("")
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public Cliente alterar(Cliente cliente) {
  
          cliente = this.clienteControle.alterar(cliente);
  
          try {
              this.clienteControle.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return cliente;
      }
  
      @GET
      @Path("/{id}")
      @Produces(MediaType.APPLICATION_JSON)
      public Cliente buscar(@PathParam("id") Integer id) {
          Cliente cliente = new Cliente();
          cliente.setIdcliente(id);
          cliente = this.clienteControle.buscar(cliente);
  
          try {
              this.clienteControle.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return cliente;
      }
  
      @DELETE
      @Path("/{id}")
      public void remover(@PathParam("id") Integer id) {
  
          Cliente cliente = new Cliente();
          cliente.setIdcliente(id);
          cliente = this.clienteControle.remover(cliente);
  
          try {
              this.clienteControle.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>ClienteControle</p>
  
  <pre><code>package br.com.controlevendas.controle;
  
  import java.io.Closeable;
  import java.util.List;
  
  import br.com.controlevendas.modelo.Cliente;
  
  public interface ClienteControle extends Closeable {
  
      Cliente salvar(Cliente cliente);
  
      Cliente alterar(Cliente cliente);
  
      Cliente remover(Cliente cliente);
  
      Cliente buscar(Cliente cliente);
  
      List&lt;Cliente&gt; listar();
  
  }
  </code></pre>
  
  <p>ClienteControleImplementacao</p>
  
  <pre><code>package br.com.controlevendas.controle.implementacao;
  
  import java.io.IOException;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Propagation;
  import org.springframework.transaction.annotation.Transactional;
  
  import br.com.controlevendas.controle.ClienteControle;
  import br.com.controlevendas.dao.ClienteDAO;
  import br.com.controlevendas.modelo.Cliente;
  
  @Service
  public class ClienteControleImplementacao implements ClienteControle {
  
      @Autowired
      private ClienteDAO clienteDAO;
  
      @Override
      public void close() throws IOException {
          this.clienteDAO.close();
      }
  
      @Transactional(propagation = Propagation.REQUIRED)
      @Override
      public Cliente salvar(Cliente cliente) {
  
          return this.clienteDAO.salvar(cliente);
      }
  
      @Transactional(propagation = Propagation.REQUIRED)
      @Override
      public Cliente alterar(Cliente cliente) {
  
          return this.clienteDAO.alterar(cliente);
      }
  
      @Override
      public Cliente buscar(Cliente cliente) {
  
          return this.clienteDAO.buscar(cliente);
      }
  
      @Override
      public List&lt;Cliente&gt; listar() {
  
          return this.clienteDAO.listar();
      }
  
      @Transactional(propagation = Propagation.REQUIRED)
      @Override
      public Cliente remover(Cliente cliente) {
  
          return this.clienteDAO.remover(cliente);
      }    
  }
  </code></pre>
  
  <p>ClienteDAO</p>
  
  <pre><code>package br.com.controlevendas.dao;
  
  import java.io.Closeable;
  import java.util.List;
  
  import br.com.controlevendas.modelo.Cliente;
  
  public interface ClienteDAO extends Closeable {
  
      Cliente salvar(Cliente cliente);
  
      Cliente alterar(Cliente cliente);
  
      Cliente remover(Cliente cliente);
  
      Cliente buscar(Cliente cliente);
  
      List&lt;Cliente&gt; listar();
  }
  </code></pre>
  
  <p>ClienteDAOImplementacao</p>
  
  <pre><code>package br.com.controlevendas.dao.implementacao;
  
  import java.io.IOException;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import org.springframework.stereotype.Repository;
  
  import br.com.controlevendas.dao.ClienteDAO;
  import br.com.controlevendas.modelo.Cliente;
  
  @Repository
  public class ClienteDAOImplementacao implements ClienteDAO {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Override
      public Cliente salvar(Cliente cliente) {
  
          this.entityManager.persist(cliente);
  
          return cliente;
      }
  
      @Override
      public Cliente alterar(Cliente cliente) {
  
          return this.entityManager.merge(cliente);
      }
  
      @Override
      public Cliente buscar(Cliente cliente) {
          Query query = this.entityManager.createQuery("from Cliente where idcliente = ?");
  
          query.setParameter(1, cliente.getIdcliente());
  
          return (Cliente) query.getSingleResult();
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public List&lt;Cliente&gt; listar() {
  
          Query query = this.entityManager.createQuery("from Cliente");
  
          return query.getResultList();
      }
  
      @Override
      public void close() throws IOException {
  
          this.entityManager.close();
      }
  
      @Override
      public Cliente remover(Cliente cliente) {
  
          cliente = this.entityManager.getReference(Cliente.class, cliente.getIdcliente());
  
          this.entityManager.remove(cliente);
  
          return cliente;
      }
  
  }
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>   &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
     http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"&gt;
  
       &lt;persistence-unit name="controlevendas"&gt;
  
         &lt;!-- provedor/implementacao do JPA --&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
       &lt;/persistence-unit&gt;
      &lt;/persistence&gt;
  </code></pre>
  
  <p>WebContent/META-INF/context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;Context reladable="true"&gt;
      &lt;Resource 
          name="jdbc/vendas"
          auth="Container"
          type="javax.sql.DataSource"
          maxActive="100"
          maxIdle="30"
          maxWait="60"
          username="root"
          password="root"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost/controlevendas"/&gt;
  
  &lt;/Context&gt;
  </code></pre>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
  
          &lt;context:annotation-config /&gt; 
  
          &lt;context:component-scan base-package="br.com.controlevendas.controle.implementacao" /&gt;
          &lt;context:component-scan base-package="br.com.controlevendas.dao.implementacao" /&gt;
          &lt;context:component-scan base-package="br.com.controlevendas.recurso" /&gt;  
  
  
      &lt;!-- &lt;bean id="dataSourceMySQL" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt; --&gt;
          &lt;!-- &lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;
  
          &lt;property name="jdbcUrl" value="jdbc:mysql://localhost/controlevendas" /&gt;
          &lt;property name="user" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
          &lt;property name="minPoolSize" value="5" /&gt;
          &lt;property name="maxPoolSize" value="20" /&gt;
          &lt;property name="maxStatements" value="50" /&gt;
          &lt;property name="checkoutTimeout" value="3000" /&gt;
          &lt;property name="idleConnectionTestPeriod" value="3000" /&gt;
          &lt;property name="maxIdleTime" value="180" /&gt;
          &lt;property name="numHelperThreads" value="5" /&gt;
          &lt;property name="acquireIncrement" value="1" /&gt;
          &lt;property name="acquireRetryAttempts" value="5" /&gt; --&gt;
      &lt;!-- &lt;/bean&gt; --&gt;
  
  
      &lt;bean id="dataSource.jndi" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true"&gt;
          &lt;property name="jndiName" value="java:comp/env/jdbc/vendas" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory"    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;!-- &lt;property name="dataSource" ref="dataSourceMySQL" /&gt; --&gt;
  
          &lt;property name="dataSource" ref="dataSource.jndi" /&gt;
  
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean
                  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="database" value="MYSQL" /&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="generateDdl" value="false" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
                  &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cglib.use_reflection_optimizer"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.use_streams_for_binary"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.temp.use_jdbc_metadata_defaults"&gt;false&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;0&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.release_mode"&gt;after_transaction&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.use_get_generated_keys"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.statement_cache.size"&gt;0&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;    
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt;
    &lt;display-name&gt;ControleVendas&lt;/display-name&gt; 
  
    &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  
    &lt;servlet&gt;
          &lt;servlet-name&gt;jersey-spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
                       com.sun.jersey.spi.spring.container.servlet.SpringServlet
          &lt;/servlet-class&gt;
          &lt;init-param&gt;
               &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
               &lt;param-value&gt;br.com.controlevendas.recurso&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;jersey-spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
        &lt;resource-ref&gt;
            &lt;description&gt;DataSource Vendas&lt;/description&gt;
            &lt;res-ref-name&gt;jdbc/vendas&lt;/res-ref-name&gt;
            &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
            &lt;res-auth&gt;Container&lt;/res-auth&gt;
        &lt;/resource-ref&gt;
  
        &lt;session-config&gt;
            &lt;session-timeout&gt;1&lt;/session-timeout&gt;
        &lt;/session-config&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>hibernate 4.3.8
  spring 4.14
  jersey 1.18
  jackson 1.9.13</p>
  
  <p>Someone help with any tips?</p>