public void add(final T rec) { if (doneAdding) { throw new IllegalStateException("Cannot add after calling doneAdding()"); } if (iterationStarted) { throw new IllegalStateException("Cannot add after calling iterator()"); } if (numRecordsInRam == maxRecordsInRam) { spillToDisk(); } ramRecords[numRecordsInRam++] = rec; }
/** * This method can be called after caller is done adding to collection, in order to possibly free * up memory. If iterator() is called immediately after caller is done adding, this is not necessary, * because iterator() triggers the same freeing. */ public void doneAdding() { if (this.cleanedUp) { throw new IllegalStateException("Cannot call doneAdding() after cleanup() was called."); } if (doneAdding) { return; } doneAdding = true; if (this.files.isEmpty()) { return; } if (this.numRecordsInRam > 0) { spillToDisk(); } // Facilitate GC this.ramRecords = null; }
/** * This method can be called after caller is done adding to collection, in order to possibly free * up memory. If iterator() is called immediately after caller is done adding, this is not necessary, * because iterator() triggers the same freeing. */ public void doneAdding() { if (this.cleanedUp) { throw new IllegalStateException("Cannot call doneAdding() after cleanup() was called."); } if (doneAdding) { return; } doneAdding = true; if (this.files.isEmpty()) { return; } if (this.numRecordsInRam > 0) { spillToDisk(); } // Facilitate GC this.ramRecords = null; }
/** * This method can be called after caller is done adding to collection, in order to possibly free * up memory. If iterator() is called immediately after caller is done adding, this is not necessary, * because iterator() triggers the same freeing. */ public void doneAdding() { if (this.cleanedUp) { throw new IllegalStateException("Cannot call doneAdding() after cleanup() was called."); } if (doneAdding) { return; } doneAdding = true; if (this.files.isEmpty()) { return; } if (this.numRecordsInRam > 0) { spillToDisk(); } // Facilitate GC this.ramRecords = null; }
public void add(final T rec) { if (doneAdding) { throw new IllegalStateException("Cannot add after calling doneAdding()"); } if (iterationStarted) { throw new IllegalStateException("Cannot add after calling iterator()"); } if (numRecordsInRam == maxRecordsInRam) { long startMem = 0; if (printRecordSizeSampling) { // Garbage collect and get free memory Runtime.getRuntime().gc(); startMem = Runtime.getRuntime().freeMemory(); } spillToDisk(); if (printRecordSizeSampling) { //Garbage collect again and get free memory Runtime.getRuntime().gc(); long endMem = Runtime.getRuntime().freeMemory(); long usedBytes = endMem - startMem; log.debug(String.format("%d records in ram required approximately %s memory or %s per record. ", maxRecordsInRam, StringUtil.humanReadableByteCount(usedBytes), StringUtil.humanReadableByteCount(usedBytes / maxRecordsInRam))); } } ramRecords[numRecordsInRam++] = rec; }
public void add(final T rec) { if (doneAdding) { throw new IllegalStateException("Cannot add after calling doneAdding()"); } if (iterationStarted) { throw new IllegalStateException("Cannot add after calling iterator()"); } if (numRecordsInRam == maxRecordsInRam) { long startMem = 0; if (printRecordSizeSampling) { // Garbage collect and get free memory Runtime.getRuntime().gc(); startMem = Runtime.getRuntime().freeMemory(); } spillToDisk(); if (printRecordSizeSampling) { //Garbage collect again and get free memory Runtime.getRuntime().gc(); long endMem = Runtime.getRuntime().freeMemory(); long usedBytes = endMem - startMem; log.debug(String.format("%d records in ram required approximately %s memory or %s per record. ", maxRecordsInRam, StringUtil.humanReadableByteCount(usedBytes), StringUtil.humanReadableByteCount(usedBytes / maxRecordsInRam))); } } ramRecords[numRecordsInRam++] = rec; }
@Test public void spillToDiskTest() { final SortingCollection<String> sortingCollection = makeSortingCollection(10); final String[] strings = new String[] { "1", "2", "3" }; for (String str : strings) { sortingCollection.add(str); } Assert.assertEquals(tmpDir().list().length, 0); sortingCollection.spillToDisk(); Assert.assertEquals(tmpDir().list().length, 1); assertIteratorEqualsList(strings, sortingCollection.iterator()); sortingCollection.cleanup(); Assert.assertEquals(tmpDir().list().length, 0); }