View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
34   *
35   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
36   *
37   */
38  public class DefaultObjectSizeEstimator implements ObjectSizeEstimator {
39  
40      private final ConcurrentMap<Class<?>, Integer> class2size =
41          new ConcurrentIdentityWeakKeyHashMap<Class<?>, Integer>();
42  
43      /**
44       * Creates a new instance.
45       */
46      public DefaultObjectSizeEstimator() {
47          class2size.put(boolean.class, 4); // Probably an integer.
48          class2size.put(byte.class, 1);
49          class2size.put(char.class, 2);
50          class2size.put(int.class, 4);
51          class2size.put(short.class, 2);
52          class2size.put(long.class, 8);
53          class2size.put(float.class, 4);
54          class2size.put(double.class, 8);
55          class2size.put(void.class, 0);
56      }
57  
58      public int estimateSize(Object o) {
59          if (o == null) {
60              return 8;
61          }
62  
63          int answer = 8 + estimateSize(o.getClass(), null);
64  
65          if (o instanceof EstimatableObjectWrapper) {
66              answer += estimateSize(((EstimatableObjectWrapper) o).unwrap());
67          } else if (o instanceof MessageEvent) {
68              answer += estimateSize(((MessageEvent) o).getMessage());
69          } else if (o instanceof ChannelBuffer) {
70              answer += ((ChannelBuffer) o).capacity();
71          } else if (o instanceof byte[]) {
72              answer += ((byte[]) o).length;
73          } else if (o instanceof ByteBuffer) {
74              answer += ((ByteBuffer) o).remaining();
75          } else if (o instanceof CharSequence) {
76              answer += ((CharSequence) o).length() << 1;
77          } else if (o instanceof Iterable<?>) {
78              for (Object m : (Iterable<?>) o) {
79                  answer += estimateSize(m);
80              }
81          }
82  
83          return align(answer);
84      }
85  
86      private int estimateSize(Class<?> clazz, Set<Class<?>> visitedClasses) {
87          Integer objectSize = class2size.get(clazz);
88          if (objectSize != null) {
89              return objectSize;
90          }
91  
92          if (visitedClasses != null) {
93              if (visitedClasses.contains(clazz)) {
94                  return 0;
95              }
96          } else {
97              visitedClasses = new HashSet<Class<?>>();
98          }
99  
100         visitedClasses.add(clazz);
101 
102         int answer = 8; // Basic overhead.
103         for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
104             Field[] fields = c.getDeclaredFields();
105             for (Field f : fields) {
106                 if ((f.getModifiers() & Modifier.STATIC) != 0) {
107                     // Ignore static fields.
108                     continue;
109                 }
110 
111                 answer += estimateSize(f.getType(), visitedClasses);
112             }
113         }
114 
115         visitedClasses.remove(clazz);
116 
117         // Some alignment.
118         answer = align(answer);
119 
120         // Put the final answer.
121         class2size.putIfAbsent(clazz, answer);
122         return answer;
123     }
124 
125     private static int align(int size) {
126         int r = size % 8;
127         if (r != 0) {
128             size += 8 - r;
129         }
130         return size;
131     }
132 }