Hi Ming Qin,
if you'll fix the test like:
for (int i = 0; i < 3; i++) {
Buffer b = mm.allocate(100);
+++ b.allowBufferDispose(true);
compositeBuffer.append(b);
}
it will work :))
Anyway, I've added your unit test to the build.
Thank you.
WBR,
Alexey.
On Mar 27, 2010, at 18:54 , Ming Qin wrote:
>
> Hi Oleksiy Stashok:
>
> Adding one Junit testing case for DefaultMemoryManagerTest
> ( revision 4355 -https://grizzly.dev.java.net/svn/grizzly/branches/2dot0)
> , testing shows the only last ByteBuffer in CompositeBuffer is
> disposed. This caused by DefaultMemeoryManager.java’s release
> method which is created for Buffer’s dispose method.
> The patch for CompositeBuffer dispose is listed in below
>
> Index: code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> MemoryManager.java
> ===================================================================
> --- code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> MemoryManager.java (revision 4376)
> +++ code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> MemoryManager.java (working copy)
> @@ -80,4 +80,6 @@
> * @param buffer {_at_link Buffer} to be released.
> */
> public void release(E buffer);
> +
> + public void release02(E buffer);
> }
> Index: code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> BuffersBuffer.java
> ===================================================================
> --- code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> BuffersBuffer.java (revision 4376)
> +++ code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> BuffersBuffer.java (working copy)
> @@ -167,6 +167,16 @@
>
> @Override
> public void dispose() {
> +
> + Object[] objects = this.underlying();
> + int length = objects.length-2;
> + for ( Object a : objects){
> +
> + if ( a==null ) continue;
> +
> + memoryManager.release02( (ByteBufferWrapper)a);
> + }
> checkDispose();
> isDisposed = true;
> removeBuffers(true);
> Index: code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> ByteBufferManager.java
> ===================================================================
> --- code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> ByteBufferManager.java (revision 4376)
> +++ code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> ByteBufferManager.java (working copy)
> @@ -101,6 +101,12 @@
> releaseByteBuffer(buffer.underlying());
> }
>
> + public void release02(ByteBufferWrapper buffer) {
> + releaseByteBuffer02(buffer.underlying());
> + }
> +
> +
> +
> /**
> * Returns <tt>true</tt>, if <tt>ByteBufferManager</tt> works
> with direct
> * {_at_link ByteBuffer}s, or <tt>false</tt> otherwise.
> @@ -197,4 +203,7 @@
> @Override
> public void releaseByteBuffer(ByteBuffer byteBuffer) {
> }
> +
> + public void releaseByteBuffer02(ByteBuffer byteBuffer) {
> + }
> }
> Index: code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> DefaultMemoryManager.java
> ===================================================================
> --- code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> DefaultMemoryManager.java (revision 4376)
> +++ code/modules/grizzly/src/main/java/com/sun/grizzly/memory/
> DefaultMemoryManager.java (working copy)
> @@ -225,8 +225,24 @@
> super.releaseByteBuffer(byteBuffer);
> }
>
> + public void releaseByteBuffer02(ByteBuffer byteBuffer) {
> + if (isDefaultWorkerThread()) {
> + ThreadLocalPool memoryPool = getThreadLocalPool();
>
> + if (memoryPool.release02((ByteBuffer)
> byteBuffer.clear())) {
> + if (memoryProbe != null) {
> +
> memoryProbe.releaseBufferToPoolEvent(byteBuffer.capacity());
> + }
>
> + return;
> + }
> + }
> +
> + super.releaseByteBuffer(byteBuffer);
> + }
> +
> +
> +
> /**
> * Get the size of local thread memory pool.
> *
> @@ -321,6 +337,19 @@
> return false;
> }
>
> + public boolean release02(ByteBuffer underlyingBuffer) {
> + if ( true) {
> + pool.position(pool.position() -
> underlyingBuffer.capacity());
> + allocationHistory[--lastAllocatedIndex] = null;
> +
> + return true;
> + } else if (tryReset(underlyingBuffer)) {
> + return true;
> + }
> +
> + return false;
> + }
> +
> public boolean tryReset(ByteBuffer byteBuffer) {
> if (wantReset(byteBuffer.remaining())) {
> reset(byteBuffer);
>
>
> Below is Junit Testing code:
> public void testCompositeBufferDispose() throws Exception {
> final DefaultMemoryManager mm = new DefaultMemoryManager(
> new MyMemoryMonitoringProbe());
> Runnable r = new Runnable() {
> @Override
> public void run() {
> // Initialize memory manager
>
> mm.allocate(0);
>
> final int initialSize = mm.getReadyThreadBufferSize();
>
> BuffersBuffer compositeBuffer =
> BuffersBuffer.create(mm);
>
> for (int i = 0; i <3; i++) {
> Buffer b = mm.allocate(100);
> compositeBuffer.append(b);
> }
>
> compositeBuffer.toByteBuffer(0, 100);
> compositeBuffer.position( 100);
>
> compositeBuffer.dispose();
>
>
> assertEquals(initialSize,
> mm.getReadyThreadBufferSize());
> }
> };
>
> testInWorkerThread(r);
> }
>
>
>
>
> Ming Qin
> Cell Phone 858-353-2839
>