📄 directbytebufferpool.java
字号:
}
}
/**
* Force system garbage collection.
*/
private void runGarbageCollection() {
if( DEBUG_PRINT_MEM ) {
System.out.println( "runGarbageCollection()" );
}
System.runFinalization();
System.gc();
}
/**
* Checks memory usage of free buffers in buffer pools,
* and calls the compaction method if necessary.
*/
private void checkMemoryUsage() {
long bytesUsed = 0;
synchronized( poolsLock ) {
//count up total bytes used by free buffers
Iterator it = buffersMap.keySet().iterator();
while (it.hasNext()) {
Integer keyVal = (Integer)it.next();
ArrayList bufferPool = (ArrayList)buffersMap.get(keyVal);
bytesUsed += keyVal.intValue() * bufferPool.size();
}
//compact buffer pools if they use too much memory
if (bytesUsed > MAX_FREE_BYTES) {
compactFreeBuffers(bytesUsed);
}
}
}
/**
* Fairly removes free buffers from the pools to limit memory usage.
*/
private void compactFreeBuffers(final long bytes_used) {
final int numPools = buffersMap.size();
long bytesToFree = 0;
int maxPoolSize = 0;
int[] buffSizes = new int[numPools];
int[] poolSizes = new int[numPools];
int[] numToFree = new int[numPools];
//fill size arrays
int pos = 0;
Iterator it = buffersMap.keySet().iterator();
while (it.hasNext()) {
Integer keyVal = (Integer)it.next();
ArrayList bufferPool = (ArrayList)buffersMap.get(keyVal);
buffSizes[pos] = keyVal.intValue();
poolSizes[pos] = bufferPool.size();
numToFree[pos] = 0;
//find initial max value
if (poolSizes[pos] > maxPoolSize) maxPoolSize = poolSizes[pos];
pos++;
}
//calculate the number of buffers to free from each pool
while( bytesToFree < (bytes_used - MAX_FREE_BYTES) ) {
for (int i=0; i < numPools; i++) {
//if the pool size is as large as the current max size
if (poolSizes[i] == maxPoolSize) {
//update counts
numToFree[i]++;
poolSizes[i]--;
bytesToFree += buffSizes[i];
}
}
//reduce max size for next round
maxPoolSize--;
}
//free buffers from the pools
pos = 0;
it = buffersMap.values().iterator();
while (it.hasNext()) {
//for each pool
ArrayList bufferPool = (ArrayList)it.next();
synchronized( bufferPool ) {
int size = bufferPool.size();
//remove the buffers from the end
for (int i=(size - 1); i >= (size - numToFree[pos]); i--) {
bufferPool.remove(i);
}
}
pos++;
}
runGarbageCollection();
}
protected void
returnBuffer(
ByteBuffer buffer )
{
bytesIn += buffer.capacity();
if ( DEBUG_TRACK_HANDEDOUT ){
synchronized( handed_out ){
if ( handed_out.remove( buffer ) == null ){
Debug.out( "buffer not handed out" );
throw( new RuntimeException( "Buffer not handed out" ));
}
// System.out.println( "[" + handed_out.size() + "] <- " + buffer + ", bytesIn = " + bytesIn + ", bytesOut = " + bytesOut );
}
}
// remInUse( buffer.capacity() );
free( buffer );
}
private long bytesFree() {
long bytesUsed = 0;
synchronized( poolsLock ) {
//count up total bytes used by free buffers
Iterator it = buffersMap.keySet().iterator();
while (it.hasNext()) {
Integer keyVal = (Integer)it.next();
ArrayList bufferPool = (ArrayList)buffersMap.get(keyVal);
bytesUsed += keyVal.intValue() * bufferPool.size();
}
}
return bytesUsed;
}
/*
private final HashMap in_use_counts = new HashMap();
private void addInUse( int size ) {
Integer key = new Integer( size );
synchronized( in_use_counts ) {
Integer count = (Integer)in_use_counts.get( key );
if( count == null ) count = new Integer( 1 );
else count = new Integer( count.intValue() + 1 );
in_use_counts.put( key, count );
}
}
private void remInUse( int size ) {
Integer key = new Integer( size );
synchronized( in_use_counts ) {
Integer count = (Integer)in_use_counts.get( key );
if( count == null ) System.out.println("count = null");
if( count.intValue() == 0 ) System.out.println("count = 0");
in_use_counts.put( key, new Integer( count.intValue() - 1 ) );
}
}
private void printInUse() {
synchronized( in_use_counts ) {
for( Iterator i = in_use_counts.keySet().iterator(); i.hasNext(); ) {
Integer key = (Integer)i.next();
int count = ((Integer)in_use_counts.get( key )).intValue();
int size = key.intValue();
if( count > 0 ) {
if( size < 1024 ) System.out.print("[" +size+ " x " +count+ "] ");
else System.out.print("[" +size/1024+ "K x " +count+ "] ");
}
}
System.out.println();
}
}
*/
private void
printInUse(
boolean verbose )
{
if ( DEBUG_PRINT_MEM ){
CacheFileManager cm = null;
try{
cm = CacheFileManagerFactory.getSingleton();
}catch( Throwable e ){
Debug.printStackTrace( e );
}
synchronized( handed_out ){
Iterator it = handed_out.values().iterator();
Map cap_map = new TreeMap();
Map alloc_map = new TreeMap();
while( it.hasNext()){
DirectByteBuffer db = (DirectByteBuffer)it.next();
if ( verbose ){
String trace = db.getTraceString();
if ( trace != null ){
System.out.println( trace );
}
}
Integer cap = new Integer( db.getBufferInternal().capacity());
Byte alloc = new Byte( db.getAllocator());
myInteger c = (myInteger)cap_map.get(cap);
if ( c == null ){
c = new myInteger();
cap_map.put( cap, c );
}
c.value++;
myInteger a = (myInteger)alloc_map.get(alloc);
if ( a == null ){
a = new myInteger();
alloc_map.put( alloc, a );
}
a.value++;
}
it = cap_map.keySet().iterator();
while( it.hasNext()){
Integer key = (Integer)it.next();
myInteger count = (myInteger)cap_map.get( key );
if( key.intValue() < 1024 ){
System.out.print("[" +key.intValue()+ " x " +count.value+ "] ");
}else{
System.out.print("[" +key.intValue()/1024+ "K x " +count.value+ "] ");
}
}
System.out.print( " - " );
it = alloc_map.keySet().iterator();
while( it.hasNext()){
Byte key = (Byte)it.next();
myInteger count = (myInteger)alloc_map.get( key );
System.out.print("[" + DirectByteBuffer.AL_DESCS[key.intValue()]+ " x " +count.value+ "] ");
}
if ( cm != null ){
CacheFileManagerStats stats = cm.getStats();
System.out.print( " - Cache: " );
System.out.print( "sz=" + stats.getSize());
System.out.print( ",us=" + stats.getUsedSize());
System.out.print( ",cw=" + stats.getBytesWrittenToCache());
System.out.print( ",cr=" + stats.getBytesReadFromCache());
System.out.print( ",fw=" + stats.getBytesWrittenToFile());
System.out.print( ",fr=" + stats.getBytesReadFromFile());
}
System.out.println();
}
}
}
protected static class
myInteger
{
int value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -