private void cleanup ( ) { if ( ( ++ counter & 0xFF ) != 0 ) { // (++counter % 256) != 0 return ; } final long currentTimeNanos = System . nanoTime ( ) ; if ( currentTimeNanos - lastCleanupTimeNanos < CLEANUP_INTERVAL_NANOS ) { return ; } for ( final Iterator < State > i = map . values ( ) . iterator ( ) ; i . hasNext ( ) ; ) { final State state = i . next ( ) ; final boolean remove ; synchronized ( state ) { remove = state . allActiveRequests == 0 && currentTimeNanos - state . lastActivityTimeNanos >= CLEANUP_INTERVAL_NANOS ; } if ( remove ) { i . remove ( ) ; } } lastCleanupTimeNanos = System . nanoTime ( ) ; }
Cleans up empty entries with no activity for more than 1 minute . For reduced overhead we perform this only when 1 ) the last clean - up was more than 1 minute ago and 2 ) the number of acquisitions % 256 is 0 .