Skip to content

Commit 989e7fd

Browse files
committed
change buffer pool size management to take available memory into consideration
1 parent f79653e commit 989e7fd

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/main/com/mongodb/ByteDecoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ protected void done(){
4949
_pool.done( this );
5050
}
5151

52-
private final static int _poolSize = 6 * BUFS_PER_50M;
53-
private final static SimplePool<ByteDecoder> _pool = new SimplePool<ByteDecoder>( "ByteDecoders" , _poolSize , -1 ){
52+
final static SimplePool<ByteDecoder> _pool = new SimplePool<ByteDecoder>( "ByteDecoders" , NUM_ENCODERS * 3 , -1 ){
5453

5554
protected ByteDecoder createNew(){
5655
if ( D ) System.out.println( "creating new ByteDecoder" );

src/main/com/mongodb/ByteEncoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ protected void done(){
9999
_pool.done( this );
100100
}
101101

102-
private final static int _poolSize = Math.min( Bytes.CONNECTIONS_PER_HOST , 2 * BUFS_PER_50M );
103-
private final static SimplePool<ByteEncoder> _pool = new SimplePool<ByteEncoder>( "ByteEncoders" , _poolSize , -1 ){
102+
final static SimplePool<ByteEncoder> _pool = new SimplePool<ByteEncoder>( "ByteEncoders" , NUM_ENCODERS , -1 ){
104103
protected ByteEncoder createNew(){
105104
if ( D ) System.out.println( "creating new ByteEncoder" );
106105
return new ByteEncoder();

src/main/com/mongodb/Bytes.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,21 @@ public class Bytes {
3434
public static final ByteOrder ORDER = ByteOrder.LITTLE_ENDIAN;
3535

3636
static final int BUF_SIZE = 1024 * 1024 * 5;
37-
3837
static final int CONNECTIONS_PER_HOST = 10;
39-
static final int BUFS_PER_50M = ( 1024 * 1024 * 50 ) / BUF_SIZE;
38+
static final int NUM_ENCODERS;
39+
40+
static {
41+
Runtime r = Runtime.getRuntime();
42+
int numBufs = (int)(r.maxMemory() / BUF_SIZE);
43+
numBufs = numBufs / 5;
44+
if ( numBufs > CONNECTIONS_PER_HOST ){
45+
numBufs = CONNECTIONS_PER_HOST;
46+
}
47+
if ( numBufs == 0 )
48+
throw new IllegalStateException( "the mongo driver doesn't have enough memory to create its buffers" );
49+
50+
NUM_ENCODERS = numBufs;
51+
}
4052

4153
static final byte EOO = 0;
4254
static final byte NUMBER = 1;

0 commit comments

Comments
 (0)