2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.nio.ByteBuffer;
23 import org.eclipse.jetty.util.LeakDetector;
24 import org.eclipse.jetty.util.component.ContainerLifeCycle;
25 import org.eclipse.jetty.util.log.Log;
26 import org.eclipse.jetty.util.log.Logger;
28 public class LeakTrackingByteBufferPool extends ContainerLifeCycle implements ByteBufferPool
30 private static final Logger LOG = Log.getLogger(LeakTrackingByteBufferPool.class);
32 private final LeakDetector<ByteBuffer> leakDetector = new LeakDetector<ByteBuffer>()
35 protected void leaked(LeakInfo leakInfo)
37 LeakTrackingByteBufferPool.this.leaked(leakInfo);
41 private final ByteBufferPool delegate;
43 public LeakTrackingByteBufferPool(ByteBufferPool delegate)
45 this.delegate = delegate;
46 addBean(leakDetector);
51 public ByteBuffer acquire(int size, boolean direct)
53 ByteBuffer buffer = delegate.acquire(size, direct);
54 if (!leakDetector.acquired(buffer))
55 LOG.warn("ByteBuffer {}@{} not tracked", buffer, System.identityHashCode(buffer));
60 public void release(ByteBuffer buffer)
64 if (!leakDetector.released(buffer))
65 LOG.warn("ByteBuffer {}@{} released but not acquired", buffer, System.identityHashCode(buffer));
66 delegate.release(buffer);
69 protected void leaked(LeakDetector<ByteBuffer>.LeakInfo leakInfo)
71 LOG.warn("ByteBuffer " + leakInfo.getResourceDescription() + " leaked at:", leakInfo.getStackFrames());