1 21 22 package nu.xom.benchmarks; 23 24 import java.io.IOException ; 25 import java.text.DecimalFormat ; 26 27 import nu.xom.Attribute; 28 import nu.xom.Builder; 29 import nu.xom.Document; 30 import nu.xom.Element; 31 import nu.xom.ParsingException; 32 33 44 class MemoryTest { 45 46 public static Document makeFullUnicode() { 47 48 Element root = new Element("root"); 49 Document doc = new Document(root); 50 51 for (int i = 0x20; i <= 0xD7FF; i++) { 52 Element data = new Element("d"); 53 data.appendChild(((char) i) + ""); 54 data.addAttribute(new Attribute("c", String.valueOf(i))); 55 root.appendChild(data); 56 } 57 58 60 for (int i = 0xE000; i <= 0xFFFD; i++) { 61 Element data = new Element("d"); 62 data.appendChild(((char) i) + ""); 63 data.addAttribute(new Attribute("c", String.valueOf(i))); 64 root.appendChild(data); 65 } 66 67 System.gc(); 68 69 for (int i = 0; i < 256; i++) { 74 int u = 0x1D100 + i; 76 int uprime = u - 0x10000; 77 int W1 = 0xD800; 78 int W2 = 0xDC00; 79 W2 = W2 | (uprime & 0x7FF ); 80 W1 = W1 | (uprime & 0xFF800); 81 Element data = new Element("d"); 82 data.appendChild( ((char) W1) + "" + ((char) W2) ); 83 data.addAttribute(new Attribute("c", String.valueOf(u))); 84 root.appendChild(data); 85 } 86 87 return doc; 88 89 } 90 91 public static void main(String [] args) 92 throws IOException , ParsingException { 93 94 DecimalFormat format = new DecimalFormat (); 95 format.setMaximumFractionDigits(2); 96 Runtime r = Runtime.getRuntime(); 97 System.gc(); System.gc(); System.gc(); 98 long before = r.totalMemory() - r.freeMemory(); 99 Document doc; 100 if (args.length > 0) { 101 Builder builder = new Builder(); 102 doc = builder.build(args[0]); 103 builder = null; 104 } 105 else { 106 doc = makeFullUnicode(); 107 } 108 long after = r.totalMemory() - r.freeMemory(); 109 double usage = (after - before)/(1024.0*1024.0); 110 System.out.println("Memory used: " 111 + format.format(usage) + "M"); 112 System.gc(); System.gc(); System.gc(); 113 long postGC = r.totalMemory() - r.freeMemory(); 114 usage = (postGC - before)/(1024.0*1024.0); 115 System.out.println("Memory used after garbage collection: " 116 + format.format(usage) + "M"); 117 118 System.out.println("Meaningless number: " 120 + doc.toXML().length()); 121 } 122 123 } 124 | Popular Tags |