Buggy code:
public class MockRAMDirectory extends RAMDirectory { long maxSize ; long maxUsedSize ; double randomIOExceptionRate ; Random randomState ; boolean noDeleteOpenFile = true ; boolean preventDoubleWrite = true ; private Set < String > unSyncedFiles ; private Set < String > createdFiles ; volatile boolean crashed ; Map < String , Integer > openFiles ; private synchronized void init ( ) { } public MockRAMDirectory ( ) { } public MockRAMDirectory ( Directory dir ) throws IOException { } public void setPreventDoubleWrite ( boolean value ) { } @ Override public synchronized void sync ( String name ) throws IOException { } public synchronized void crash ( ) throws IOException { } public synchronized void clearCrash ( ) throws IOException { } public void setMaxSizeInBytes ( long maxSize ) { } public long getMaxSizeInBytes ( ) { } public long getMaxUsedSizeInBytes ( ) { } public void resetMaxUsedSizeInBytes ( ) { } public void setNoDeleteOpenFile ( boolean value ) { } public boolean getNoDeleteOpenFile ( ) { } public void setRandomIOExceptionRate ( double rate , long seed ) { } public double getRandomIOExceptionRate ( ) { } void maybeThrowIOException ( ) throws IOException { } @ Override public synchronized void deleteFile ( String name ) throws IOException { } private synchronized void deleteFile ( String name , boolean forced ) throws IOException { } @ Override public synchronized IndexOutput createOutput ( String name ) throws IOException { if ( crashed ) throw new IOException ( "cannot<seq2seq4repair_space>createOutput<seq2seq4repair_space>after<seq2seq4repair_space>crash" ) ; init ( ) ; if ( ( ( preventDoubleWrite ) && ( createdFiles . contains ( name ) ) ) && ( ! ( name . equals ( "segments.gen" ) ) ) ) throw new IOException ( ( ( "file<seq2seq4repair_space>\"" + name ) + "\"<seq2seq4repair_space>was<seq2seq4repair_space>already<seq2seq4repair_space>written<seq2seq4repair_space>to" ) ) ; if ( ( noDeleteOpenFile ) && ( openFiles . containsKey ( name ) ) ) throw new IOException ( ( ( "MockRAMDirectory:<seq2seq4repair_space>file<seq2seq4repair_space>\"" + name ) + "\"<seq2seq4repair_space>is<seq2seq4repair_space>still<seq2seq4repair_space>open:<seq2seq4repair_space>cannot<seq2seq4repair_space>overwrite" ) ) ; RAMFile file = new RAMFile ( this ) ; if ( crashed ) throw new IOException ( "cannot<seq2seq4repair_space>createOutput<seq2seq4repair_space>after<seq2seq4repair_space>crash" ) ; unSyncedFiles . add ( name ) ; createdFiles . add ( name ) ; RAMFile existing = fileMap . get ( name ) ; if ( ( ( existing != null ) && ( ! ( name . equals ( "segments.gen" ) ) ) ) && ( preventDoubleWrite ) ) throw new IOException ( ( ( "file<seq2seq4repair_space>" + name ) + "<seq2seq4repair_space>already<seq2seq4repair_space>exists" ) ) ; else { if ( existing != null ) { <START_BUG> sizeInBytes -= existing . sizeInBytes ; <END_BUG> existing . directory = null ; } fileMap . put ( name , file ) ; } return new MockRAMOutputStream ( this , file , name ) ; } @ Override public synchronized IndexInput openInput ( String name ) throws IOException { } public final synchronized long getRecomputedSizeInBytes ( ) { } public final synchronized long getRecomputedActualSizeInBytes ( ) { } @ Override public synchronized void close ( ) { } public static class Failure { public void eval ( MockRAMDirectory dir ) throws IOException { } public MockRAMDirectory . Failure reset ( ) { } protected boolean doFail ; public void setDoFail ( ) { } public void clearDoFail ( ) { } } ArrayList failures ; public synchronized void failOn ( MockRAMDirectory . Failure fail ) { } synchronized void maybeThrowDeterministicException ( ) throws IOException { } }

Fix:
sizeInBytes . getAndAdd ( ( - ( existing . sizeInBytes ) ) ) ;