KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > benchmark > MemoryUsage


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove.benchmark;
20
21 import gnu.trove.*;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 public class MemoryUsage {
29     public static long sizeOf(Creator c) {
30         long size= 0;
31         Object JavaDoc[] objects = new Object JavaDoc[100];
32         try {
33             Object JavaDoc 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 JavaDoc 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 JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71
72     public static void main(String JavaDoc[] 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 JavaDoc e) {
99             e.printStackTrace();
100         }
101         System.exit(0);
102     }
103
104     static class MemoryComparator {
105         Creator trove, javasoft;
106         String JavaDoc description;
107         MemoryComparator(Creator trove,
108                          Creator javasoft,
109                          String JavaDoc 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 JavaDoc create();
133     }
134
135     static class TroveIntArrayListCreator implements Creator {
136         public Object JavaDoc 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 JavaDoc create() {
148             ArrayList JavaDoc list = new ArrayList JavaDoc();
149             for (int i = 0; i < 1000; i++) {
150                 Integer JavaDoc x = new Integer JavaDoc(i);
151                 list.add(x);
152             }
153             list.trimToSize();
154             return list;
155         }
156     }
157
158     static class TroveMapCreator implements Creator {
159         public Object JavaDoc create() {
160             THashMap map = new THashMap();
161             for (int i = 0; i < 1000; i++) {
162                 Integer JavaDoc x = new Integer JavaDoc(i);
163                 map.put(x,x);
164             }
165             return map;
166         }
167     }
168
169     static class JavasoftMapCreator implements Creator {
170         public Object JavaDoc create() {
171             HashMap JavaDoc map = new HashMap JavaDoc();
172             for (int i = 0; i < 1000; i++) {
173                 Integer JavaDoc x = new Integer JavaDoc(i);
174                 map.put(x,x);
175             }
176             return map;
177         }
178     }
179
180     static class TroveSetCreator implements Creator {
181         public Object JavaDoc create() {
182             THashSet map = new THashSet();
183             for (int i = 0; i < 1000; i++) {
184                 Integer JavaDoc x = new Integer JavaDoc(i);
185                 map.add(x);
186             }
187             return map;
188         }
189     }
190
191     static class JavasoftSetCreator implements Creator {
192         public Object JavaDoc create() {
193             HashSet JavaDoc map = new HashSet JavaDoc();
194             for (int i = 0; i < 1000; i++) {
195                 Integer JavaDoc x = new Integer JavaDoc(i);
196                 map.add(x);
197             }
198             return map;
199         }
200     }
201
202     static class TroveListCreator implements Creator {
203         public Object JavaDoc 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 JavaDoc create() {
215             LinkedList JavaDoc list = new LinkedList JavaDoc();
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