1 19 package gnu.trove.benchmark; 20 21 import gnu.trove.*; 22 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 28 public class MemoryUsage { 29 public static long sizeOf(Creator c) { 30 long size= 0; 31 Object [] objects = new Object [100]; 32 try { 33 Object primer = c.create(); 34 long startingMemoryUse = getUsedMemory(); 35 for (int i = 0; i < objects.length; i++) { 36 objects[i] = c.create(); 37 } 38 long endingMemoryUse = getUsedMemory(); 39 float approxSize = (endingMemoryUse - 40 startingMemoryUse) / 100f; 41 size = Math.round(approxSize); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 return size; 46 } 47 48 private static long getUsedMemory() { 49 gc(); 50 long totalMemory = Runtime.getRuntime().totalMemory(); 51 gc(); 52 long freeMemory = Runtime.getRuntime().freeMemory(); 53 long usedMemory = totalMemory - freeMemory; 54 return usedMemory; 55 } 56 57 private static void gc() { 58 try { 59 System.gc(); 60 Thread.currentThread().sleep(100); 61 System.runFinalization(); 62 Thread.currentThread().sleep(100); 63 System.gc(); 64 Thread.currentThread().sleep(100); 65 System.runFinalization(); 66 Thread.currentThread().sleep(100); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 } 70 } 71 72 public static void main(String [] args) { 73 try { 74 MemoryComparator set = 75 new MemoryComparator(new TroveSetCreator(), 76 new JavasoftSetCreator(), 77 "Compare size of Set implementation: 1,000 Integer objects measured in bytes"); 78 set.compare(); 79 set = null; 80 MemoryComparator list = 81 new MemoryComparator(new TroveListCreator(), 82 new JavasoftListCreator(), 83 "Compare size of LinkedList implementation: 1,000 TLinkableAdaptor objects measured in bytes"); 84 list.compare(); 85 list = null; 86 MemoryComparator list2 = 87 new MemoryComparator(new TroveIntArrayListCreator(), 88 new JavasoftIntegerArrayListCreator(), 89 "Compare size of int/IntegerArrayList implementation: 1,000 ints measured in bytes"); 90 list2.compare(); 91 list2 = null; 92 93 MemoryComparator map = 94 new MemoryComparator(new TroveMapCreator(), 95 new JavasoftMapCreator(), 96 "Compare size of Map implementation: 1,000 Integer->Integer mappings measured in bytes"); 97 map.compare(); 98 } catch (Exception e) { 99 e.printStackTrace(); 100 } 101 System.exit(0); 102 } 103 104 static class MemoryComparator { 105 Creator trove, javasoft; 106 String description; 107 MemoryComparator(Creator trove, 108 Creator javasoft, 109 String description) { 110 this.trove = trove; 111 this.javasoft = javasoft; 112 this.description = description; 113 } 114 115 public void compare() { 116 gc(); 117 long j = sizeOf(javasoft); 118 gc(); 119 long t = sizeOf(trove); 120 121 long p = Math.round(t * 100 / j * 100) / 100; 122 123 System.out.println("--------------------------"); 124 System.out.println(description); 125 System.out.println("javasoft: " + j); 126 System.out.println("trove: " + t); 127 System.out.println("trove's collection requires " + p + "% of the memory needed by javasoft's collection"); 128 } 129 } 130 131 interface Creator { 132 Object create(); 133 } 134 135 static class TroveIntArrayListCreator implements Creator { 136 public Object create() { 137 TIntArrayList list = new TIntArrayList(); 138 for (int i = 0; i < 1000; i++) { 139 list.add(i); 140 } 141 list.trimToSize(); 142 return list; 143 } 144 } 145 146 static class JavasoftIntegerArrayListCreator implements Creator { 147 public Object create() { 148 ArrayList list = new ArrayList (); 149 for (int i = 0; i < 1000; i++) { 150 Integer x = new Integer (i); 151 list.add(x); 152 } 153 list.trimToSize(); 154 return list; 155 } 156 } 157 158 static class TroveMapCreator implements Creator { 159 public Object create() { 160 THashMap map = new THashMap(); 161 for (int i = 0; i < 1000; i++) { 162 Integer x = new Integer (i); 163 map.put(x,x); 164 } 165 return map; 166 } 167 } 168 169 static class JavasoftMapCreator implements Creator { 170 public Object create() { 171 HashMap map = new HashMap (); 172 for (int i = 0; i < 1000; i++) { 173 Integer x = new Integer (i); 174 map.put(x,x); 175 } 176 return map; 177 } 178 } 179 180 static class TroveSetCreator implements Creator { 181 public Object create() { 182 THashSet map = new THashSet(); 183 for (int i = 0; i < 1000; i++) { 184 Integer x = new Integer (i); 185 map.add(x); 186 } 187 return map; 188 } 189 } 190 191 static class JavasoftSetCreator implements Creator { 192 public Object create() { 193 HashSet map = new HashSet (); 194 for (int i = 0; i < 1000; i++) { 195 Integer x = new Integer (i); 196 map.add(x); 197 } 198 return map; 199 } 200 } 201 202 static class TroveListCreator implements Creator { 203 public Object create() { 204 TLinkedList list = new TLinkedList(); 205 for (int i = 0; i < 1000; i++) { 206 TLinkableAdapter a = new TLinkableAdapter(); 207 list.add(a); 208 } 209 return list; 210 } 211 } 212 213 static class JavasoftListCreator implements Creator { 214 public Object create() { 215 LinkedList list = new LinkedList (); 216 for (int i = 0; i < 1000; i++) { 217 TLinkableAdapter a = new TLinkableAdapter(); 218 list.add(a); 219 } 220 return list; 221 } 222 } 223 } 224 | Popular Tags |