clear() not longer frees memory it just empties the list
Use case: The list is used as a buffer, that is re-used and in each iteration the list is cleared. Now that clear() does not replace the data array there is no garbage collection and we do not have to allocated one or several new arrays in the next iteration. You can still free the associated memory by calling clear() + trim().
This commit is contained in:
@@ -589,13 +589,12 @@ public final class IntList implements Serializable, Cloneable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from this list.
|
||||
* Removes all elements from the list.
|
||||
* <p>
|
||||
* The implementation is equivalent to calling {@code remove(0, size())} and
|
||||
* {@code trim()}.
|
||||
* This method does not free any memory associated with this list. Call
|
||||
* {@link #clear()} + {@link #trim()} to free associated memory.
|
||||
*/
|
||||
public void clear() {
|
||||
data = EMPTY_ARRAY;
|
||||
size = 0;
|
||||
sorted = true;
|
||||
}
|
||||
|
||||
@@ -575,13 +575,12 @@ public final class LongList implements Serializable, Cloneable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from this list.
|
||||
* Removes all elements from the list.
|
||||
* <p>
|
||||
* The implementation is equivalent to calling {@code remove(0, size())} and
|
||||
* {@code trim()}.
|
||||
* This method does not free any memory associated with this list. Call
|
||||
* {@link #clear()} + {@link #trim()} to free associated memory.
|
||||
*/
|
||||
public void clear() {
|
||||
data = EMPTY_ARRAY;
|
||||
size = 0;
|
||||
sorted = true;
|
||||
}
|
||||
|
||||
@@ -509,11 +509,12 @@ public class IntListTest {
|
||||
@Test
|
||||
public void testClear() {
|
||||
final IntList list = IntList.of(2, 0, 1); // unsorted list
|
||||
final int capacityBeforeClear = list.getCapacity();
|
||||
list.clear();
|
||||
|
||||
Assertions.assertEquals(0, list.size());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
Assertions.assertEquals(0, list.size(), "the list is empty after clear");
|
||||
Assertions.assertEquals(capacityBeforeClear, capacityBeforeClear, "capacity does not change");
|
||||
Assertions.assertTrue(list.isSorted(), "empty lists are sorted");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unlikely-arg-type")
|
||||
@@ -744,7 +745,7 @@ public class IntListTest {
|
||||
final IntList list = new IntList();
|
||||
list.addAll(-2, -1, 0, 1, 2);
|
||||
Assertions.assertEquals("[-2, -1, 0, 1, 2]", list.toString());
|
||||
Assertions.assertEquals("same result as Arrays.toString()", Arrays.toString(list.toArray()), list.toString());
|
||||
Assertions.assertEquals(Arrays.toString(list.toArray()), list.toString(), "same result as Arrays.toString()");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -786,9 +787,9 @@ public class IntListTest {
|
||||
.boxed()//
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assertions.assertEquals("should be sequential, when using collect", actualList.toString(), list.toString());
|
||||
Assertions.assertNotEquals("should use parallelism during computation", processingOrder.toString(),
|
||||
list.toString());
|
||||
Assertions.assertEquals(actualList.toString(), list.toString(), "should be sequential, when using collect");
|
||||
Assertions.assertNotEquals(processingOrder.toString(), list.toString(),
|
||||
"should use parallelism during computation");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -509,11 +509,12 @@ public class LongListTest {
|
||||
@Test
|
||||
public void testClear() {
|
||||
final LongList list = LongList.of(2, 0, 1); // unsorted list
|
||||
final int capacityBeforeClear = list.getCapacity();
|
||||
list.clear();
|
||||
|
||||
Assertions.assertEquals(0, list.size());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
Assertions.assertEquals(0, list.size(), "the list is empty after clear");
|
||||
Assertions.assertEquals(capacityBeforeClear, capacityBeforeClear, "capacity does not change");
|
||||
Assertions.assertTrue(list.isSorted(), "empty lists are sorted");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unlikely-arg-type")
|
||||
@@ -744,7 +745,7 @@ public class LongListTest {
|
||||
final LongList list = new LongList();
|
||||
list.addAll(-2, -1, 0, 1, 2);
|
||||
Assertions.assertEquals("[-2, -1, 0, 1, 2]", list.toString());
|
||||
Assertions.assertEquals("same result as Arrays.toString()", Arrays.toString(list.toArray()), list.toString());
|
||||
Assertions.assertEquals(Arrays.toString(list.toArray()), list.toString(), "same result as Arrays.toString()");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -786,9 +787,9 @@ public class LongListTest {
|
||||
.boxed()//
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assertions.assertEquals("should be sequential, when using collect", actualList.toString(), list.toString());
|
||||
Assertions.assertNotEquals("should use parallelism during computation", processingOrder.toString(),
|
||||
list.toString());
|
||||
Assertions.assertEquals(actualList.toString(), list.toString(), "should be sequential, when using collect");
|
||||
Assertions.assertNotEquals(processingOrder.toString(), list.toString(),
|
||||
"should use parallelism during computation");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user