// Populate ArrayList<String> two different ways:
//
// loop1 uses a single common String for each element that is added
//
// loop2 adds a String created on the fly. The String itself
// is identical to the common String used in loop 1
//
// Zaphod_b
import java.util.*;
import java.lang.*;
public class Z
{
public static void main(String[] args) throws Exception
{
long total, free, size0, size1, size2;
int number = 1000000;
String string1 = "cat";
String string2 = "dog";
String string3 = "rabbit";
String string4 = "mouse";
String string5 = "eagle";
String line = string1 + " " + string2 + " " + string3 + " " + string4 + " " + string5;
// Show memory parameters before the fun begins
total = Runtime.getRuntime().totalMemory();
free = Runtime.getRuntime().freeMemory();
size0 = total-free;
System.out.printf("Initially : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size0);
System.out.println();
System.out.printf("Creating ArrayList<String> objects with %d elements\n", number);
System.out.printf("Length of string = %d\n\n", line.length());
// First method: will put the "line" String in all of the elements
ArrayList<String> list = loop1(number, line);
total = Runtime.getRuntime().totalMemory();
free = Runtime.getRuntime().freeMemory();
size1 = total-free;
System.out.printf("After loop1 : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size1);
// How many bytes used in creating the ArrayList with 'number' elements
System.out.printf(" delta = %10d Bytes\n", size1-size0);
// Is this meaningful? How many bytes memory for each element. I think so.
System.out.printf(" delta / n = %10.6f\n", (double)(size1-size0)/number);
System.out.println();
// loop2 puts in the same string, but creates it on the fly
// for each one.
ArrayList<String> list2 = loop2(number);
total = Runtime.getRuntime().totalMemory(); // Bytes
free = Runtime.getRuntime().freeMemory(); // Bytes
size2 = total-free;
// Summary for this one
System.out.printf("After loop2 : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size2);
System.out.printf(" delta = %10d Bytes\n", size2-size1);
System.out.printf(" delta / n = %10.6f\n", (double)(size2-size1)/number);
System.out.println();
}
// Add a fixed String each time
static ArrayList<String> loop1(int n, String line) {
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
long size0 = total-free;
System.out.printf("Entry into Loop1 : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size0);
System.out.println();
// Allocate storage for length n
ArrayList<String> list = new ArrayList<String>(n);
total = Runtime.getRuntime().totalMemory();
free = Runtime.getRuntime().freeMemory();
long size1 = total-free;
System.out.printf("After allocation : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size1);
System.out.printf(" delta = %10d Bytes\n", size1-size0);
System.out.println();
for(int i = 0; i < n; i++)
{
list.add(line);
}
return list;
}
// Add a temporary String each time
static ArrayList<String> loop2(int n) throws Exception {
String string1 = "cat";
String string2 = "dog";
String string3 = "rabbit";
String string4 = "mouse";
String string5 = "eagle";
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
long size0 = total-free;
System.out.printf("Entry into Loop2 : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size0);
System.out.println();
// Allocate storage for length n
ArrayList<String> list = new ArrayList<String>(n);
total = Runtime.getRuntime().totalMemory();
free = Runtime.getRuntime().freeMemory();
long size1 = total-free;
System.out.printf("After allocation : totalMemory = %10d Bytes\n", total);
System.out.printf(" freeMemory = %10d Bytes\n", free);
System.out.printf(" difference = %10d Bytes\n", size1);
System.out.printf(" delta = %10d Bytes\n", size1-size0);
System.out.println();
for(int i = 0; i < n; i++)
{
list.add(string1 + " " + string2 + " " + string3 + " " + string4 + " " + string5);
}
return list;
}
}