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)

scope guessDeepObjectSize_debug.

sclass #Path {
  O parentObject;
  Field field;
  Path parent;
  
  *(O *parentObject, Field *field, Path *parent) {}
  
  toString {
    ret (parent == null ? "" : parent + " -> ")
      //+ field;
      + className(parentObject) + (field == null ? "" : "." + field.getName());
  }
}

sclass OnStack {
  O object;
  Path path; // how we got to this object
  
  *(O *object, Path *path) {}
}

sclass #Data {
  long size;
  new L<OnStack> stack;
  Set seen = identityHashSet(); // add objects here to ignore them
  Set<Field> fieldsToIgnore;
  Set<S> realmsToIgnore;
}

static long guessDeepObjectSize_debug(O o) {
  if (o == null) ret 0;
  ret guessDeepObjectSize_debug(new Data, o);
}

static long guessDeepObjectSize_debug(Data data, O o) {
  if (o == null) ret 0;
  data.stack.add(OnStack(o, null));
  while ping (nempty(data.stack)) {
    #step(data, popLast(data.stack));
  }
  ret data.size;
}

svoid #step(Data data, OnStack os) {
  O o = os.object;
  if (!data.seen.add(o)) ret;
  if (o instanceof Component) ret; // skipping AWT for now
  if (o instanceof Thread) ret;
  
  if (data.realmsToIgnore != null && contains(data.realmsToIgnore, getRealm(o))) ret;
  
  if (o instanceof Class) ret;

  if (isArray(o)) {
    int len;
    if (o instanceof O[]) {
      len = unsafe_sizeOf(o);
      for (O x : (O[]) o)
        if (x != null && !data.seen.contains(x)) data.stack.add(
          OnStack(x, Path(o, null, os.path)));
    } else
      len = inMemorySizeOfPrimitiveArray(o);
    data.size += len;
    ret;
  }
  
  data.size += unsafe_sizeOf(o);
  
  if (sizeCalculation_shouldSkipObject(o)) ret;
  
  try {
    for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
      if (contains(data.fieldsToIgnore, f)) continue;
      O x = f.get(o);
      if (x != null && !data.seen.contains(x)) {
        data.stack.add(OnStack(x, Path(o, f, os.path));
      }
    }
  } catch e {
    print("Error estimating object size: " + os.path + " - " + e);
  }

  //fail("can't handle object type: " + className(o));
}

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: 254 / 290
Version history: 6 change(s)
Referenced in: [show references]