1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.util;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Modifier;
20 import java.nio.ByteBuffer;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentMap;
24
25 import org.jboss.netty.buffer.ChannelBuffer;
26 import org.jboss.netty.channel.MessageEvent;
27 import org.jboss.netty.util.internal.ConcurrentIdentityWeakKeyHashMap;
28
29
30
31
32 public class DefaultObjectSizeEstimator implements ObjectSizeEstimator {
33
34 private final ConcurrentMap<Class<?>, Integer> class2size =
35 new ConcurrentIdentityWeakKeyHashMap<Class<?>, Integer>();
36
37
38
39
40 public DefaultObjectSizeEstimator() {
41 class2size.put(boolean.class, 4);
42 class2size.put(byte.class, 1);
43 class2size.put(char.class, 2);
44 class2size.put(int.class, 4);
45 class2size.put(short.class, 2);
46 class2size.put(long.class, 8);
47 class2size.put(float.class, 4);
48 class2size.put(double.class, 8);
49 class2size.put(void.class, 0);
50 }
51
52 public int estimateSize(Object o) {
53 if (o == null) {
54 return 8;
55 }
56
57 int answer = 8 + estimateSize(o.getClass(), null);
58
59 if (o instanceof EstimatableObjectWrapper) {
60 answer += estimateSize(((EstimatableObjectWrapper) o).unwrap());
61 } else if (o instanceof MessageEvent) {
62 answer += estimateSize(((MessageEvent) o).getMessage());
63 } else if (o instanceof ChannelBuffer) {
64 answer += ((ChannelBuffer) o).capacity();
65 } else if (o instanceof byte[]) {
66 answer += ((byte[]) o).length;
67 } else if (o instanceof ByteBuffer) {
68 answer += ((ByteBuffer) o).remaining();
69 } else if (o instanceof CharSequence) {
70 answer += ((CharSequence) o).length() << 1;
71 } else if (o instanceof Iterable<?>) {
72 for (Object m : (Iterable<?>) o) {
73 answer += estimateSize(m);
74 }
75 }
76
77 return align(answer);
78 }
79
80 private int estimateSize(Class<?> clazz, Set<Class<?>> visitedClasses) {
81 Integer objectSize = class2size.get(clazz);
82 if (objectSize != null) {
83 return objectSize;
84 }
85
86 if (visitedClasses != null) {
87 if (visitedClasses.contains(clazz)) {
88 return 0;
89 }
90 } else {
91 visitedClasses = new HashSet<Class<?>>();
92 }
93
94 visitedClasses.add(clazz);
95
96 int answer = 8;
97 for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
98 Field[] fields = c.getDeclaredFields();
99 for (Field f : fields) {
100 if ((f.getModifiers() & Modifier.STATIC) != 0) {
101
102 continue;
103 }
104
105 answer += estimateSize(f.getType(), visitedClasses);
106 }
107 }
108
109 visitedClasses.remove(clazz);
110
111
112 answer = align(answer);
113
114
115 class2size.putIfAbsent(clazz, answer);
116 return answer;
117 }
118
119 private static int align(int size) {
120 int r = size % 8;
121 if (r != 0) {
122 size += 8 - r;
123 }
124 return size;
125 }
126 }