Under high load we have CPU usage ~90% and GC spending most of time collecting garbage. Most of this garbage is BasicDBObject objects, which we use only as intermediate objects in mapping from BSON stream to our domain objects.
I came up with the following not very clean solution:
I create a wrapper for BSON:
class BsonDbObject implements DBObject {
private byte[] data;
// empty implementations for other methods
}
custom DBDecoder with one overridden method:
class BsonDbDecoder implements DBDecoder {
@Override
public DBObject decode(InputStream in, DBCollection collection) throws IOException {
byte[] data = fullRead(in);
return new BsonDbObject(data);
}
// empty implementations for other methods
}
and build MongoClient with our custom BsonDbDecoderFactory. Later in the code I perform mapping directly from BSON stream.
This solution is quite complex and not stable yet, i.e. $errors handling in DBObject is still not implemented.
Is there more stable and elegant way to skip creating DBObjects in mongo-java-driver to reduce load on GC (as I know 3.0.x allows it but we use 2.12.4)?