My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (c) 2011. Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/

package com.google.code.java.core.sizeof;

import org.joda.time.DateTime;
import org.junit.Test;

import java.util.AbstractMap;
import java.util.BitSet;
import java.util.Calendar;
import java.util.concurrent.atomic.AtomicReference;

import static junit.framework.Assert.assertEquals;

public class SizeofUtilTest {
@Test
public void testAverageBytes() throws Exception {
assertEquals(4.0, new SizeofUtil() {
int[] array;

@Override
protected int create() {
array = new int[1024];
return array.length;
}
}.averageBytes(), 0.02);

assertEquals(1.0 / 8, new SizeofUtil() {
BitSet bits;

@Override
protected int create() {
bits = new BitSet(1024 * 1024);
return bits.size();
}
}.averageBytes(), 1e-4);
}

@Test
public void testHeaderSize() {
System.out.printf("The average size of an int is %.1f bytes%n", new SizeofUtil() {
int[] obj = null;

@Override
protected int create() {
obj = new int[1024];
return obj.length;
}
}.averageBytes());
System.out.printf("The average size of an Object is %.1f bytes%n", new SizeofUtil() {
Object obj = null;

@Override
protected int create() {
obj = new Object();
return 1;
}
}.averageBytes());
System.out.printf("The average size of an Integer is %.1f bytes%n", new SizeofUtil() {
Integer obj = null;

@Override
protected int create() {
obj = new Integer(1);
return 1;
}
}.averageBytes());
System.out.printf("The average size of a Long is %.1f bytes%n", new SizeofUtil() {
Long obj = null;

@Override
protected int create() {
obj = new Long(1);
return 1;
}
}.averageBytes());
System.out.printf("The average size of an AtomicReference is %.1f bytes%n", new SizeofUtil() {
AtomicReference obj = null;

@Override
protected int create() {
obj = new AtomicReference();
return 1;
}
}.averageBytes());
System.out.printf("The average size of an SimpleEntry(Map.Entry) is %.1f bytes%n", new SizeofUtil() {
AbstractMap.SimpleEntry obj = null;

@Override
protected int create() {
obj = new AbstractMap.SimpleEntry(null, null);
return 1;
}
}.averageBytes());
System.out.printf("The average size of a DateTime is %.1f bytes%n", new SizeofUtil() {
DateTime obj = null;

@Override
protected int create() {
obj = new DateTime();
return 1;
}
}.averageBytes());
System.out.printf("The average size of a Calendar is %.1f bytes%n", new SizeofUtil() {
Calendar obj = null;

@Override
protected int create() {
obj = Calendar.getInstance();
return 1;
}
}.averageBytes());
System.out.printf("The average size of an Exception is %.1f bytes%n", new SizeofUtil() {
Exception obj = null;

@Override
protected int create() {
obj = new Exception("" + System.currentTimeMillis());
return 1;
}
}.averageBytes());
System.out.printf("The average size of a bit in a BitSet is %.3f bytes%n", new SizeofUtil() {
BitSet obj = null;

@Override
protected int create() {
obj = new BitSet(128 * 1024);
return obj.size();
}
}.averageBytes());
}
}

Change log

r58 by peter.lawrey on Aug 30, 2011   Diff
Beerware licensing.
Go to: 
Project members, sign in to write a code review

Older revisions

r31 by peter.lawrey on Jul 23, 2011   Diff
Added Joda DateTime
r25 by peter.lawrey on Jul 17, 2011   Diff
Sizeof example
r24 by peter.lawrey on Jul 16, 2011   Diff
Sizeof example
All revisions of this file

File info

Size: 3857 bytes, 139 lines
Powered by Google Project Hosting