title: Tomcat session is not deleted even after invalidating http session in spring web application, this is causing memory leak
body: <p>I have invalidated session in <code>HandlerInterceptorAdapter</code>, Please refer the below code.</p>
  
  <pre><code>@Override
  public void postHandle(HttpServletRequest request,
          HttpServletResponse response, Object handler,
          ModelAndView modelAndView) throws Exception {   request.getSession(false).invalidate();
      log.debug ("Session Deleted after invalidate:" + request.getSession(false));
  
  }
  </code></pre>
  
  <p>Here I am seeing <code>null</code> in log. That means session is destroyed. Please correct me if I am wrong.</p>
  
  <p>Now the problem is the PS Old Gen memory is growing, I have taken a dump from the server and I found there are lot of memory blocked by <code>StandardSession</code> related objects.</p>
  
  <p><a href="https://i.stack.imgur.com/mYapG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mYapG.png" alt="enter image description here"></a></p>
  
  <p>In the above image <code>ConcurrentHashMap</code> has lot of session id vs <code>StandardSession</code> object data. </p>
  
  <p>Can someone please explain me why this session objects are not cleared from the memory even after invalidating the session.</p>
  
  <pre><code>@Override
  public boolean preHandle(HttpServletRequest request,
          HttpServletResponse response, Object handler) throws Exception {
      createSession();    
      return setupSession(request);
  }
  
  
  private void createSession() {      
      ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
      if(sra != null){
          HttpServletRequest req = sra.getRequest();    
          if(req != null){
              HttpSession session =  req.getSession(false);
              if(session != null) {
                  log.debug("Invalidating old session");
                  session.invalidate();
              }           
              session = req.getSession(true);
              session.setAttribute(StrutsSettingsHandler.REQUEST_TYPE_REST_KEY, 
                      StrutsSettingsHandler.REQUEST_TYPE_REST);
  
              session.setMaxInactiveInterval(10);
              log.debug ("New session created, Id = "+session.getId());                                       
          }
      }       
  }
  </code></pre>