Skip to content

Commit

Permalink
Improve calculation of available memory when sizing buffers in Sortin…
Browse files Browse the repository at this point in the history
…gCollection. (#1437)
  • Loading branch information
tfenne authored Nov 25, 2019
1 parent a5335ef commit 33988ba
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/htsjdk/samtools/util/SortingCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,16 @@ class MergingIterator implements CloseableIterator<T> {
// enough memory for buffering it will return zero and all reading will be unbuffered.
private int checkMemoryAndAdjustBuffer(int numFiles) {
int bufferSize = Defaults.BUFFER_SIZE;

// garbage collect so that our calculation is accurate.
Runtime.getRuntime().gc();
final Runtime rt = Runtime.getRuntime();
rt.gc();

// free in heap space available to expand heap
final long allocatableMemory = rt.freeMemory() + (rt.maxMemory() - rt.totalMemory());

// There is ~20k in overhead per file.
final long freeMemory = Runtime.getRuntime().freeMemory() - (numFiles * 20 * 1024);
final long freeMemory = allocatableMemory - (numFiles * 20 * 1024);
// use the floor value from the divide
final int memoryPerFile = (int) (freeMemory / numFiles);

Expand Down

0 comments on commit 33988ba

Please sign in to comment.