2
* Created on Nov 4, 2008
3
* Created by Paul Gardner
5
* Copyright 2008 Vuze, Inc. All rights reserved.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License only.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22
package org.gudy.azureus2.core3.util;
25
import java.nio.ByteBuffer;
28
DirectByteBufferPoolHeap
29
extends DirectByteBufferPool
31
private final boolean USE_POOLS = false;
33
private final boolean TRACE = false;
35
private final int MIN_POOL;
36
private final int MAX_POOL;
39
private final LinkedList[] pools;
40
private final int[] pool_sizes;
45
DirectByteBufferPoolHeap()
48
* On linux (at least) JDK1.7.0 using heap buffers doesn't play well as the JVM still
49
* goes and creates DirectByteBuffers under the covers and things run out of control
50
* (after a few mins I have a 9GB VM...)
55
pool_sizes = new int[]{
65
MIN_POOL = pool_sizes[0];
66
MAX_POOL = pool_sizes[pool_sizes.length-1];
68
pools = new LinkedList[ pool_sizes.length ];
70
for (int i=0;i<pools.length;i++){
72
pools[i] = new LinkedList();
77
sizes = new TreeMap();
79
SimpleTimer.addPeriodicEvent(
82
new TimerEventPerformer()
88
synchronized( sizes ){
92
Iterator it = sizes.entrySet().iterator();
96
Map.Entry entry = (Map.Entry)it.next();
98
str += (str.length()==0?"":",") + entry.getKey() + "->" + entry.getValue();
101
System.out.println( "HB allocs: " + str );
108
MIN_POOL = MAX_POOL = 0;
115
protected DirectByteBuffer
124
synchronized( sizes ){
126
Integer key = new Integer( length/32*32 );
128
Integer count = (Integer)sizes.get( key );
130
if ( count == null ){
132
sizes.put( key, new Integer(1));
136
sizes.put( key, new Integer( count.intValue() + 1 ));
141
int pool_index = getPoolIndex( length );
143
if ( pool_index != -1 ){
145
LinkedList pool = pools[pool_index];
147
synchronized( pool ){
149
if ( !pool.isEmpty()){
151
Object[] entry = (Object[])pool.removeLast();
153
ByteBuffer buff = (ByteBuffer)entry[0];
157
buff.limit( length );
159
return( new DirectByteBuffer( allocator, buff, this ));
163
DirectByteBuffer buffer = new DirectByteBuffer( allocator, ByteBuffer.allocate( pool_sizes[pool_index] ), this );
165
ByteBuffer buff = buffer.getBufferInternal();
167
buff.limit( length );
173
return( new DirectByteBuffer( allocator, ByteBuffer.allocate( length ), this ));
177
return( new DirectByteBuffer( allocator, ByteBuffer.allocate( length ), this ));
183
DirectByteBuffer buffer )
187
ByteBuffer buff = buffer.getBufferInternal();
189
int length = buff.capacity();
191
int pool_index = getPoolIndex( length );
193
if ( pool_index != -1 ){
195
LinkedList pool = pools[pool_index];
197
synchronized( pool ){
199
pool.addLast( new Object[]{ buff });
209
if ( length < MIN_POOL|| length > MAX_POOL ){
214
for (int i=0;i<pool_sizes.length;i++){
216
if ( length <= pool_sizes[i] ){