Consider the following code:
public class BitSetTest
{
public static void main(final String[] args) throws IOException
{
System.out.println("Start?");
int ch = System.in.read();
List<Integer> numbers = getSortedNumbers();
System.out.println("Generated numbers");
ch = System.in.read();
RangeSet<Integer> rangeSet = TreeRangeSet.create();
for (Integer number : numbers)
{
rangeSet.add(Range.closed(number, number));
}
System.out.println("Finished rangeset");
ch = System.in.read();
BitSet bitset = new BitSet();
for (Integer number : numbers)
{
bitset.set(number.intValue());
}
System.out.println("Finished bitset");
ch = System.in.read();
}
private static List<Integer> getSortedNumbers()
{
int max = 200000000;
int n = max / 10;
List<Integer> numbers = Lists.newArrayListWithExpectedSize(max);
File file = new File("numbers.txt");
if (file.exists())
{
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
{
String line = reader.readLine();
while ((line = reader.readLine()) != null)
{
numbers.add(Integer.valueOf(line));
}
}
catch (IOException e1)
{
throw new RuntimeException(e1);
}
}
else
{
for (int i = 0; i < n; i++)
{
int number = (int) (Math.random() * max);
numbers.add(number);
if (i > 0 && i % 10000 == 0)
{
System.out.println(i);
}
}
Collections.sort(numbers);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file)))
{
writer.write(numbers.get(0) + "");
for (int i = 1; i < n; i++)
{
writer.write("\n");
writer.write(numbers.get(i) + "");
}
}
catch (IOException e1)
{
throw new RuntimeException(e1);
}
}
return numbers;
}
}
In the first pause (System.in.read ()), JConsole shows the memory usage as 4 MB. In the second pause ("Generated numbers"), since a large list is created, the memory usage goes to 922 MB. At the next pause ("Finished range") after starting the GC, the memory returns to 4 MB, which means that the list is compiled, although the function has not ended in the area.
When commented out sys outs are uncommented and used, the list is not compiled until sysout is executed.
, JVM , , ?