KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > test > XMLMemTest


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP [See end of
3  * file] $Id: XMLMemTest.java,v 1.4 2005/02/21 12:11:26 andy_seaborne Exp $
4  */

5 package com.hp.hpl.jena.rdf.arp.test;
6
7 import org.xml.sax.*;
8 import org.xml.sax.ContentHandler JavaDoc;
9
10 import org.apache.xerces.parsers.*;
11 import java.net.*;
12
13 /**
14  * The purpose of this class is to be a minimal example which exercises the
15  * memory leak problems reported in "Streaming OWL DL" by Jeremy Carroll.
16  *
17  * I don't believe it is though ...
18  *
19  * @author jjc
20  *
21  *
22  */

23 public class XMLMemTest implements ContentHandler JavaDoc {
24     static private class DeliberateEnd extends RuntimeException JavaDoc {
25
26     }
27     final private int limit;
28     private int seen = 0;
29     private int chksum = 0;
30     Locator locator;
31     private XMLMemTest(int cnt) {
32         limit = cnt;
33     }
34     /**
35      * @param args
36      * The first arg is a URL of an XML file to read, the second is
37      * optional, and the test will abort after that number of start
38      * tags.
39      */

40
41     public static void main(String JavaDoc[] args) {
42         XMLMemTest test =
43             new XMLMemTest(args.length > 1 ? Integer.parseInt(args[1]) : -1);
44         SAXParser sax = new SAXParser();
45         sax.setContentHandler(test);
46         Runtime JavaDoc runtime = Runtime.getRuntime();
47         runtime.gc();
48         runtime.gc();
49         long startMem = runtime.totalMemory() - runtime.freeMemory();
50         try {
51             for (int i=args.length > 2 ? Integer.parseInt(args[2]) : 1;i>0;i--){
52             URL url = new URL(args[0]);
53
54             InputSource inputS = new InputSource(url.openStream());
55             inputS.setSystemId(args[0]);
56             sax.parse(inputS);
57             }
58         } catch (DeliberateEnd e) {
59
60         } catch (Exception JavaDoc e) {
61             e.printStackTrace();
62         }
63         runtime.gc();
64         runtime.gc();
65         long endMem = runtime.totalMemory() - runtime.freeMemory();
66         System.err.println("Start: "+startMem);
67         System.err.println("End: "+endMem);
68         System.err.println("Used: "+(endMem-startMem));
69         System.err.println("Count: "+test.seen);
70         System.err.println("Hash: "+test.chksum);
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
77      */

78
79     public void setDocumentLocator(Locator locator) {
80         this.locator = locator;
81         
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.xml.sax.ContentHandler#startDocument()
88      */

89     public void startDocument() throws SAXException {
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.xml.sax.ContentHandler#endDocument()
96      */

97     public void endDocument() throws SAXException {
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
104      * java.lang.String)
105      */

106     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
107         throws SAXException {
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
114      */

115     public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
116     }
117 /*
118     private void c(String s) {
119         if (s != null)
120             chksum ^= s.hashCode();
121     }
122     */

123     /*
124      * (non-Javadoc)
125      *
126      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
127      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
128      */

129     public void startElement(
130         String JavaDoc namespaceURI,
131         String JavaDoc localName,
132         String JavaDoc qName,
133         Attributes atts)
134         throws SAXException {
135         if (++seen == limit)
136             throw new DeliberateEnd();
137         locator.getLineNumber();
138         locator.getColumnNumber();
139     // if (true)
140
// return;
141
// c(namespaceURI);
142
// c(localName);
143
// c(qName);
144
// for (int i = 0; i < atts.getLength(); i++) {
145
// c(atts.getLocalName(i));
146
// c(atts.getQName(i));
147
// c(atts.getURI(i));
148
// atts.getIndex(atts.getQName(i));
149
// atts.getIndex(atts.getURI(i),atts.getLocalName(i));
150
// }
151

152     }
153
154     /*
155      * (non-Javadoc)
156      *
157      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
158      * java.lang.String, java.lang.String)
159      */

160     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
161         throws SAXException {
162     }
163
164     /*
165      * (non-Javadoc)
166      *
167      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
168      */

169     public void characters(char[] ch, int start, int length)
170         throws SAXException {
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
177      */

178     public void ignorableWhitespace(char[] ch, int start, int length)
179         throws SAXException {
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
186      * java.lang.String)
187      */

188     public void processingInstruction(String JavaDoc target, String JavaDoc data)
189         throws SAXException {
190     }
191
192     /*
193      * (non-Javadoc)
194      *
195      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
196      */

197     public void skippedEntity(String JavaDoc name) throws SAXException {
198     }
199 }
200
201 /*
202  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP All rights
203  * reserved.
204  *
205  * Redistribution and use in source and binary forms, with or without
206  * modification, are permitted provided that the following conditions are met:
207  * 1. Redistributions of source code must retain the above copyright notice,
208  * this list of conditions and the following disclaimer.
209  * 2. Redistributions in binary form must reproduce the above copyright
210  * notice, this list of conditions and the following disclaimer in the
211  * documentation and/or other materials provided with the distribution.
212  * 3. The name of the author may not be used to endorse or promote products
213  * derived from this software without specific prior written permission.
214  *
215  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
216  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
217  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
218  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
220  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
221  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
222  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
223  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
224  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225  */
Popular Tags