Jon checked the source control system and discovered a recent check-in by a new-hire touched nearly every file. The check-in comment read, “Performance enhancements; reduced memory usage.”

Runtime runny = Runtime.getRuntime();
runny.freeMemory(); //performance

/* Add the allocation level information */
TxnMaker tm = new TxnMaker(txnId, buySell, rate, taxFlag, isForeign, map);
xmlMessage.getTxns().addAll(tm.getTxns());
runny.freeMemory(); //performance

Log.writeTrace("Txns built");

TxnGroupMaker gm = new TxnGroupMaker(txnId, buySell, rate, spanishTaxFlag, isForeign, map);
Log.writeTrace("Txn Groups Built");

gm.smoothAllEconomicFields(tm.getAllocations(), map, isForeign);

xmlMessage.getTxnGroups().addAll(gm.getTxnGroups());
runny.freeMemory(); //performance

The runny.freeMemory() lines now dotted the code base like crab grass. The new-hire hoped to reduce the memory footprint by forcing garbage collection. At the same time, he could reduce the overall performance of the application by forcing the whole application to wait for the garbage collector to grind through allocated memory.

The good news: the developer failed to fail. The methodfreeMemory doesn’t do what the new-hire thought it did. From the Javadoc:

 long java.lang.Runtime.freeMemory()
   Returns the amount of free memory in the Java Virtual Machine . Calling the gc method may result in increasing the value returned by freeMemory.
   Returns: an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!