Skip to content

Commit 19e34b7

Browse files
committed
Added a few commodity methods. Removed confusing overload of sizeOf().
1 parent bdbcf21 commit 19e34b7

File tree

3 files changed

+54
-65
lines changed

3 files changed

+54
-65
lines changed

pom.xml

+1-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<!-- Project info. -->
1313
<groupId>com.carrotsearch</groupId>
1414
<artifactId>java-sizeof</artifactId>
15-
<version>0.0.1</version>
15+
<version>0.0.2</version>
1616
<packaging>jar</packaging>
1717

1818
<name>java-sizeof</name>
@@ -55,13 +55,6 @@
5555
<version>0.3.0</version>
5656
<scope>test</scope>
5757
</dependency>
58-
59-
<dependency>
60-
<groupId>org.apache.lucene</groupId>
61-
<artifactId>lucene-core</artifactId>
62-
<version>3.5.0</version>
63-
<scope>test</scope>
64-
</dependency>
6558
</dependencies>
6659

6760

src/main/java/com/carrotsearch/sizeof/RamUsageEstimator.java

+52-13
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,36 @@ public static long alignObjectSize(long size) {
290290
* should be GCed.</p>
291291
*/
292292
public static long sizeOf(Object obj) {
293-
return measureObjectSize(obj);
293+
ArrayList<Object> stack = new ArrayList<Object>();
294+
stack.add(obj);
295+
return measureSizeOf(stack);
294296
}
295297

296298
/**
297-
* Same as {@link #sizeOf(Object)} but sums up all the arguments.
299+
* Same as {@link #sizeOf(Object)} but sums up all the arguments. Not an
300+
* overload to prevent accidental parameter conflicts with {@link #sizeOf(Object)}.
298301
*/
299-
public static long sizeOf(Object... obj) {
300-
long sum = 0;
301-
for (Object o : obj) {
302-
sum += measureObjectSize(o);
302+
public static long sizeOfAll(Object... objects) {
303+
return sizeOfAll(Arrays.asList(objects));
304+
}
305+
306+
/**
307+
* Same as {@link #sizeOf(Object)} but sums up all the arguments. Not an
308+
* overload to prevent accidental parameter conflicts with {@link #sizeOf(Object)}.
309+
*/
310+
public static long sizeOfAll(Iterable<Object> objects) {
311+
final ArrayList<Object> stack;
312+
if (objects instanceof Collection<?>) {
313+
stack = new ArrayList<Object>(((Collection<?>) objects).size());
314+
} else {
315+
stack = new ArrayList<Object>();
303316
}
304-
return sum;
317+
318+
for (Object o : objects) {
319+
stack.add(o);
320+
}
321+
322+
return measureSizeOf(stack);
305323
}
306324

307325
/**
@@ -321,6 +339,28 @@ public static long shallowSizeOf(Object obj) {
321339
}
322340
}
323341

342+
/**
343+
* Same as {@link #shallowSizeOf(Object)} but sums up all the arguments. Not an
344+
* overload to prevent accidental parameter conflicts with {@link #shallowSizeOf(Object)}.
345+
*/
346+
public static long shallowSizeOfAll(Object... objects) {
347+
return shallowSizeOfAll(Arrays.asList(objects));
348+
}
349+
350+
/**
351+
* Same as {@link #shallowSizeOf(Object)} but sums up all the arguments. Duplicate
352+
* objects are not resolved and will be counted twice.
353+
* Not an overload to prevent accidental parameter conflicts with
354+
* {@link #shallowSizeOf(Object)}.
355+
*/
356+
public static long shallowSizeOfAll(Iterable<Object> objects) {
357+
long sum = 0;
358+
for (Object o : objects) {
359+
sum += shallowSizeOf(o);
360+
}
361+
return sum;
362+
}
363+
324364
/**
325365
* Returns the shallow instance size in bytes an instance of the given class would occupy.
326366
* This works with all conventional classes and primitive types, but not with arrays
@@ -378,18 +418,17 @@ private static long shallowSizeOfArray(Object array) {
378418
return alignObjectSize(size);
379419
}
380420

381-
/*
421+
/**
382422
* Non-recursive version of object descend. This consumes more memory than recursive in-depth
383423
* traversal but prevents stack overflows on long chains of objects
384424
* or complex graphs (a max. recursion depth on my machine was ~5000 objects linked in a chain
385-
* so not too much).
425+
* so not too much).
426+
*
427+
* @param stack Root objects.
386428
*/
387-
private static long measureObjectSize(Object root) {
429+
private static long measureSizeOf(ArrayList<Object> stack) {
388430
final IdentityHashSet<Object> seen = new IdentityHashSet<Object>();
389431
final IdentityHashMap<Class<?>, ClassCache> classCache = new IdentityHashMap<Class<?>, ClassCache>();
390-
final ArrayList<Object> stack = new ArrayList<Object>();
391-
392-
stack.add(root);
393432

394433
long totalSize = 0;
395434
while (!stack.isEmpty()) {

src/test/java/com/carrotsearch/sizeof/TestEstimationQuality.java

+1-44
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,6 @@ public long newObject(Object[] vault, int i) {
101101
});
102102
}
103103

104-
/**
105-
* Wild class instances (and arrays, etc.).
106-
*/
107-
@Test @Ignore
108-
public void testWildClassesOldLuceneEstimator() {
109-
estimate(new Allocator() {
110-
List<Class<?>> classes = new ArrayList<Class<?>>();
111-
HashMap<Class<?>,Long> sizes = new HashMap<Class<?>,Long>();
112-
Random random = new Random();
113-
114-
{
115-
final org.apache.lucene.util.RamUsageEstimator rue =
116-
new org.apache.lucene.util.RamUsageEstimator(false);
117-
for (Class<?> c : WildClasses.ALL) {
118-
try {
119-
long estimateRamUsage = rue.estimateRamUsage(c.newInstance());
120-
if (estimateRamUsage > 80) {
121-
continue;
122-
}
123-
classes.add(c);
124-
sizes.put(c, estimateRamUsage);
125-
} catch (Exception e) {
126-
throw new RuntimeException(e);
127-
}
128-
}
129-
}
130-
131-
@Override
132-
public long newObject(Object[] vault, int i) {
133-
Class<?> c = classes.get(random.nextInt(classes.size()));
134-
try {
135-
vault[i] = c.newInstance();
136-
} catch (Exception e) {
137-
throw new RuntimeException(e);
138-
}
139-
if (!sizes.containsKey(c)) {
140-
throw new RuntimeException();
141-
}
142-
return sizes.get(c);
143-
}
144-
});
145-
}
146-
147104
/**
148105
* Estimate estimation quality of {@link Allocator}'s objects.
149106
*/
@@ -173,7 +130,7 @@ public void estimate(Allocator allocator) {
173130

174131
Arrays.fill(vault, null);
175132

176-
long expectedFree = free - RamUsageEstimator.sizeOf(vault, allocator);
133+
long expectedFree = free - RamUsageEstimator.sizeOfAll(vault, allocator);
177134
double difference = 100.0d * (expectedAllocated - expectedFree) / (double) expectedFree;
178135
System.out.println(String.format(Locale.ENGLISH,
179136
"Expected free: %s, Allocated estimation: %s, Difference: %.2f%% (%s)",

0 commit comments

Comments
 (0)