KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > benchmarks > MemoryTest


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.benchmarks;
23
24 import java.io.IOException JavaDoc;
25 import java.text.DecimalFormat JavaDoc;
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 /**
34  * <p>
35  * Simple memory benchmark focusing on a big document
36  * full of small elements or a user supplied document.
37  * </p>
38  *
39  *
40  * @author Elliotte Rusty Harold
41  * @version 1.0
42  *
43  */

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         // skip surrogates between 0xD800 and 0xDFFF
59

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         // Plane-1 characters are tricky because Java
70
// strings encode them as surrogate pairs. First, fill
71
// a byte array with the characters from 1D100 to 1D1FF
72
// (the musical symbols)
73
for (int i = 0; i < 256; i++) {
74             // algorithm from RFC 2781
75
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 JavaDoc[] args)
92       throws IOException JavaDoc, ParsingException {
93         
94         DecimalFormat JavaDoc format = new DecimalFormat JavaDoc();
95         format.setMaximumFractionDigits(2);
96         Runtime JavaDoc 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         // Make sure the document isn't prematurely garbage collected
119
System.out.println("Meaningless number: "
120           + doc.toXML().length());
121     }
122
123 }
124
Popular Tags