View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * The default {@link ObjectSizeEstimator} implementation for general purpose.
31   */
32  public class DefaultObjectSizeEstimator implements ObjectSizeEstimator {
33  
34      private final ConcurrentMap<Class<?>, Integer> class2size =
35          new ConcurrentIdentityWeakKeyHashMap<Class<?>, Integer>();
36  
37      /**
38       * Creates a new instance.
39       */
40      public DefaultObjectSizeEstimator() {
41          class2size.put(boolean.class, 4); // Probably an integer.
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; // Basic overhead.
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                     // Ignore static fields.
102                     continue;
103                 }
104 
105                 answer += estimateSize(f.getType(), visitedClasses);
106             }
107         }
108 
109         visitedClasses.remove(clazz);
110 
111         // Some alignment.
112         answer = align(answer);
113 
114         // Put the final answer.
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 }