1 | static long dataSize(O o) { |
2 | return dataSize(o, new IdentityHashMap); |
3 | } |
4 | |
5 | static long dataSize(Object o, IdentityHashMap seen) { |
6 | if (o == null || seen.containsKey(o)) return 0; |
7 | seen.put(o, Boolean.TRUE); |
8 | long size = 12; // HotSpot header size |
9 | Class c = o.getClass(); |
10 | if (c.isArray()) { |
11 | size += 4; // array size field |
12 | int n = Array.getLength(o); |
13 | size += n * dataSize_arrayElementSize(c.getComponentType()); |
14 | if (!c.getComponentType().isPrimitive()) |
15 | for (int i = 0; i < n; i++) |
16 | size += dataSize(Array.get(o, i), seen); |
17 | } else |
18 | while (c != null) { |
19 | Field[] fields = c.getDeclaredFields(); |
20 | for (Field field : fields) { |
21 | if ((field.getModifiers() & Modifier.STATIC) != 0) |
22 | continue; |
23 | if (field.getType().isPrimitive()) |
24 | size += dataSize_primitiveSize(field.getType()); |
25 | else { |
26 | size += 4; // pointer |
27 | Object value = null; |
28 | try { |
29 | field.setAccessible(true); |
30 | value = field.get(o); |
31 | } catch (IllegalAccessException e) { |
32 | throw new RuntimeException(e); |
33 | } |
34 | size += dataSize(value, seen); |
35 | } |
36 | } |
37 | c = c.getSuperclass(); |
38 | } |
39 | return size; |
40 | } |
41 | |
42 | static long dataSize_primitiveSize(Class type) { |
43 | if (type == boolean.class) return 4; |
44 | if (type == int.class) return 4; |
45 | if (type == long.class) return 8; |
46 | if (type == float.class) return 4; |
47 | if (type == short.class) return 4; |
48 | if (type == char.class) return 4; |
49 | if (type == byte.class) return 4; |
50 | if (type == double.class) return 8; |
51 | throw fail("woot? " + type); |
52 | } |
53 | |
54 | static long dataSize_arrayElementSize(Class type) { |
55 | if (type == boolean.class) return 1; |
56 | if (type == int.class) return 4; |
57 | if (type == long.class) return 8; |
58 | if (type == float.class) return 4; |
59 | if (type == short.class) return 2; |
60 | if (type == char.class) return 2; |
61 | if (type == byte.class) return 1; |
62 | if (type == double.class) return 8; |
63 | ret 4; // pointers |
64 | } |
Began life as a copy of #1000524 Took information from http://www.slideshare.net/cnbailey/memory-efficient-java This should be correct for 32bit VMs.
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002038 |
Snippet name: | dataSize function |
Eternal ID of this version: | #1002038/1 |
Text MD5: | eedf60ad07aec2ab9dd14781fd01aedd |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-12-15 00:24:22 |
Source code size: | 2168 bytes / 64 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 711 / 948 |
Referenced in: | [show references] |