KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > test > MemoryTest


1 package org.lobobrowser.html.test;
2
3 import java.io.InputStream JavaDoc;
4 import java.net.HttpURLConnection JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.net.URLConnection JavaDoc;
7
8 import org.lobobrowser.html.*;
9 import org.lobobrowser.html.domimpl.*;
10 import org.lobobrowser.html.gui.*;
11 import org.lobobrowser.html.parser.DocumentBuilderImpl;
12 import org.lobobrowser.html.parser.InputSourceImpl;
13 import org.lobobrowser.html.renderer.*;
14 import org.lobobrowser.util.io.IORoutines;
15 import org.w3c.dom.Document JavaDoc;
16
17 import java.util.Collection JavaDoc;
18 import java.util.logging.*;
19 import java.io.*;
20 import javax.swing.*;
21 import java.awt.*;
22
23 /**
24  * Checks for memory leaks.
25  */

26 public class MemoryTest {
27     // JVM setting -Xmx150m tried with:
28
// - 500K file with fairly complex markup.
29
// - 1.5M file with simple markup.
30

31     private static final Logger logger = Logger.getLogger(MemoryTest.class.getName());
32     
33     /**
34      * @param args
35      */

36     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
37         MemoryTest mt = new MemoryTest();
38         //mt.testParserLoop();
39
//mt.testRendererLoop();
40
mt.testRendererGUILoop();
41     }
42
43     private static String JavaDoc TEST_URL = "file:c:\\temp\\html\\long.html";
44     
45     public void testParserLoop() throws Exception JavaDoc {
46         URL JavaDoc url = new URL JavaDoc(TEST_URL);
47         URLConnection JavaDoc connection = url.openConnection();
48         connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+");
49         connection.setRequestProperty("Cookie", "");
50         if(connection instanceof HttpURLConnection JavaDoc) {
51             HttpURLConnection JavaDoc hc = (HttpURLConnection JavaDoc) connection;
52             hc.setInstanceFollowRedirects(true);
53             int responseCode = hc.getResponseCode();
54             logger.info("process(): HTTP response code: " + responseCode);
55         }
56         InputStream JavaDoc in = connection.getInputStream();
57         byte[] content;
58         try {
59             content = IORoutines.load(in, 8192);
60         } finally {
61             in.close();
62         }
63         //String source = new String(content, "ISO-8859-1");
64
//long time1 = System.currentTimeMillis();
65
logger.info("Content size: " + content.length + " bytes.");
66         UserAgentContext context = new SimpleUserAgentContext();
67         DocumentBuilderImpl builder = new DocumentBuilderImpl(context);
68         for(int i = 0; i < 200; i++) {
69             logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory());
70             InputStream JavaDoc bin = new ByteArrayInputStream(content);
71             Document JavaDoc document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1"));
72             logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory() + ",document=" + document);
73             document = null;
74             System.gc();
75             logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory());
76             Thread.sleep(2);
77         }
78     }
79
80     public void testRendererLoop() throws Exception JavaDoc {
81         URL JavaDoc url = new URL JavaDoc(TEST_URL);
82         URLConnection JavaDoc connection = url.openConnection();
83         connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+");
84         connection.setRequestProperty("Cookie", "");
85         if(connection instanceof HttpURLConnection JavaDoc) {
86             HttpURLConnection JavaDoc hc = (HttpURLConnection JavaDoc) connection;
87             hc.setInstanceFollowRedirects(true);
88             int responseCode = hc.getResponseCode();
89             logger.info("process(): HTTP response code: " + responseCode);
90         }
91         InputStream JavaDoc in = connection.getInputStream();
92         byte[] content;
93         try {
94             content = IORoutines.load(in, 8192);
95         } finally {
96             in.close();
97         }
98         //String source = new String(content, "ISO-8859-1");
99
//long time1 = System.currentTimeMillis();
100
logger.info("Content size: " + content.length + " bytes.");
101         final UserAgentContext ucontext = new SimpleUserAgentContext();
102         final HtmlPanel panel = new HtmlPanel();
103         final HtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel);
104         DocumentBuilderImpl builder = new DocumentBuilderImpl(ucontext, rcontext);
105         InputStream JavaDoc bin = new ByteArrayInputStream(content);
106         final FrameContext frameContext = new LocalFrameContext();
107         final RenderableContainer renderableContainer = new LocalRenderableContainer();
108         for(int i = 0; i < 100; i++) {
109             logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory());
110             bin = new ByteArrayInputStream(content);
111             Document JavaDoc document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1"));
112             logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory());
113             {
114                 final Document JavaDoc doc = document;
115                 EventQueue.invokeAndWait(new Runnable JavaDoc() {
116                     public void run() {
117                         RBlock block = new RBlock((NodeImpl) doc, 0, rcontext.getUserAgentContext(), rcontext, frameContext, renderableContainer, RBlock.OVERFLOW_NONE);
118                         block.layout(100, 100);
119                     }
120                 });
121                 //panel.setDocument(doc, rcontext, pcontext);
122
Thread.sleep(50);
123                 //panel.clearDocument();
124
}
125             document = null;
126             System.gc();
127             logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory());
128             Thread.sleep(2);
129         }
130     }
131     
132     public void testRendererGUILoop() throws Exception JavaDoc {
133         URL JavaDoc url = new URL JavaDoc(TEST_URL);
134         URLConnection JavaDoc connection = url.openConnection();
135         connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+");
136         connection.setRequestProperty("Cookie", "");
137         if(connection instanceof HttpURLConnection JavaDoc) {
138             HttpURLConnection JavaDoc hc = (HttpURLConnection JavaDoc) connection;
139             hc.setInstanceFollowRedirects(true);
140             int responseCode = hc.getResponseCode();
141             logger.info("process(): HTTP response code: " + responseCode);
142         }
143         InputStream JavaDoc in = connection.getInputStream();
144         byte[] content;
145         try {
146             content = IORoutines.load(in, 8192);
147         } finally {
148             in.close();
149         }
150         //String source = new String(content, "ISO-8859-1");
151
//long time1 = System.currentTimeMillis();
152
logger.info("Content size: " + content.length + " bytes.");
153         final UserAgentContext ucontext = new SimpleUserAgentContext();
154         final HtmlPanel panel = new HtmlPanel();
155         final HtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel);
156         DocumentBuilderImpl builder = new DocumentBuilderImpl(ucontext, rcontext);
157         JFrame testFrame = new JFrame("Testing...");
158         testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159         testFrame.getContentPane().setLayout(new java.awt.BorderLayout JavaDoc());
160         testFrame.getContentPane().add(panel, BorderLayout.CENTER);
161         InputStream JavaDoc bin = new ByteArrayInputStream(content);
162         testFrame.setSize(new java.awt.Dimension JavaDoc(600, 400));
163         testFrame.setVisible(true);
164         for(int i = 0; i < 20; i++) {
165             logger.info("Starting parse # " + i + ": freeMemory=" + Runtime.getRuntime().freeMemory());
166             bin = new ByteArrayInputStream(content);
167             Document JavaDoc document = builder.parse(new InputSourceImpl(bin, url.toExternalForm(), "ISO-8859-1"));
168             logger.info("Finished parsing: freeMemory=" + Runtime.getRuntime().freeMemory());
169             panel.setDocument(document, rcontext);
170             EventQueue.invokeAndWait(new Runnable JavaDoc() { public void run() {} });
171             // Without these sleeps, it does apparently run out of memory.
172
Thread.sleep(3000);
173             panel.clearDocument();
174             Thread.sleep(1000);
175             document = null;
176             System.gc();
177             logger.info("After GC: freeMemory=" + Runtime.getRuntime().freeMemory());
178             Thread.sleep(2000);
179         }
180     }
181
182     private class LocalRenderableContainer implements RenderableContainer {
183         public void invalidateLayoutUpTree() {
184             // nop
185
}
186         
187         public Component add(Component component) {
188             //nop
189
return null;
190         }
191         
192         public void remove(Component c) {
193             // nop
194
}
195
196         public Color getPaintedBackgroundColor() {
197             return Color.BLACK;
198         }
199
200         public Insets getInsets() {
201             return new Insets(0, 0, 0, 0);
202         }
203
204         public void repaint(int x, int y, int width, int height) {
205         }
206
207         public void relayout() {
208             // nop
209
}
210
211         public void updateAllWidgetBounds() {
212             // nop
213
}
214
215         public Point getGUIPoint(int clientX, int clientY) {
216             return new Point(clientX, clientY);
217         }
218
219         public void focus() {
220             //nop
221
}
222
223         public void addDelayedPair(DelayedPair pair) {
224             //nop
225
}
226
227         public RenderableContainer getParentContainer() {
228             return null;
229         }
230
231         public Collection JavaDoc getDelayedPairs() {
232             return null;
233         }
234
235         public void clearDelayedPairs() {
236         }
237     }
238     
239     private class LocalFrameContext implements FrameContext {
240         public void expandSelection(RenderableSpot rpoint) {
241         }
242
243         public void resetSelection(RenderableSpot rpoint) {
244         }
245
246         public void delayedRelayout(NodeImpl node) {
247         }
248     }
249 }
250
Popular Tags