Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

88
LINES

< > BotCompany Repo | #1022101 // guessDeepObjectSize_debug [show path to problematic objects, dev.]

JavaX fragment (include)

1  
scope guessDeepObjectSize_debug.
2  
3  
sclass #Path {
4  
  O parentObject;
5  
  Field field;
6  
  Path parent;
7  
  
8  
  *(O *parentObject, Field *field, Path *parent) {}
9  
  
10  
  toString {
11  
    ret (parent == null ? "" : parent + " -> ")
12  
      //+ field;
13  
      + className(parentObject) + (field == null ? "" : "." + field.getName());
14  
  }
15  
}
16  
17  
sclass OnStack {
18  
  O object;
19  
  Path path; // how we got to this object
20  
  
21  
  *(O *object, Path *path) {}
22  
}
23  
24  
sclass #Data {
25  
  long size;
26  
  new L<OnStack> stack;
27  
  Set seen = identityHashSet(); // add objects here to ignore them
28  
  Set<Field> fieldsToIgnore;
29  
  Set<S> realmsToIgnore;
30  
}
31  
32  
static long guessDeepObjectSize_debug(O o) {
33  
  if (o == null) ret 0;
34  
  ret guessDeepObjectSize_debug(new Data, o);
35  
}
36  
37  
static long guessDeepObjectSize_debug(Data data, O o) {
38  
  if (o == null) ret 0;
39  
  data.stack.add(OnStack(o, null));
40  
  while ping (nempty(data.stack)) {
41  
    #step(data, popLast(data.stack));
42  
  }
43  
  ret data.size;
44  
}
45  
46  
svoid #step(Data data, OnStack os) {
47  
  O o = os.object;
48  
  if (!data.seen.add(o)) ret;
49  
  if (o instanceof Component) ret; // skipping AWT for now
50  
  if (o instanceof Thread) ret;
51  
  
52  
  if (data.realmsToIgnore != null && contains(data.realmsToIgnore, getRealm(o))) ret;
53  
  
54  
  if (o instanceof Class) ret;
55  
56  
  if (isArray(o)) {
57  
    int len;
58  
    if (o instanceof O[]) {
59  
      len = unsafe_sizeOf(o);
60  
      for (O x : (O[]) o)
61  
        if (x != null && !data.seen.contains(x)) data.stack.add(
62  
          OnStack(x, Path(o, null, os.path)));
63  
    } else
64  
      len = inMemorySizeOfPrimitiveArray(o);
65  
    data.size += len;
66  
    ret;
67  
  }
68  
  
69  
  data.size += unsafe_sizeOf(o);
70  
  
71  
  if (sizeCalculation_shouldSkipObject(o)) ret;
72  
  
73  
  try {
74  
    for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
75  
      if (contains(data.fieldsToIgnore, f)) continue;
76  
      O x = f.get(o);
77  
      if (x != null && !data.seen.contains(x)) {
78  
        data.stack.add(OnStack(x, Path(o, f, os.path));
79  
      }
80  
    }
81  
  } catch e {
82  
    print("Error estimating object size: " + os.path + " - " + e);
83  
  }
84  
85  
  //fail("can't handle object type: " + className(o));
86  
}
87  
88  
end scope

Author comment

Began life as a copy of #1009445

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022101
Snippet name: guessDeepObjectSize_debug [show path to problematic objects, dev.]
Eternal ID of this version: #1022101/7
Text MD5: 7cfbef9f8b1aa8d70fcc2f13dd06060b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-07 22:09:15
Source code size: 2148 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 255 / 292
Version history: 6 change(s)
Referenced in: [show references]