title: Java heap analysis with OQL/VisualVM: find path from indirect referrer to referee?
body: <p>I am trying to find the first path found from an object to some indirect referree.</p>
  
  <p>The following code is what I came up with:</p>
  
  <pre><code>var debug = [];
  
  //set referrer and target
  var referrerObj = heap.findObject("815262352");
  var targetObj = heap.findObject("815441600");
  
  //prepare refer obj
  var pathToTarget = getShortestReferPath([referrerObj], targetObj, 6);
  [allocTrace(pathToTarget), debug];
  
  //sets the shortest reference path from referrer to target
  function getShortestReferPath(referPath, targetObj, maxDepth, depth) {
      depth = (typeof(depth) != "undefined") ? depth : 1;
  
      //get the last referrer for further processing
      var lastReferrer = referPath[referPath.length-1];
  
      //check every referee of the referrer
      var dirReferees = referees(lastReferrer);
      while (dirReferees.hasMoreElements()) {
          var dirReferee = dirReferees.nextElement();
  
          //if this referee is our target, set the path and return
          if (identical(dirReferee, targetObj)) {
              referPath.push(dirReferee);
              return referPath;
          }
      }
  
      //none of these is our target, so check the next depth if it is not too high
      if (depth &lt; maxDepth) {
          var dirRefereesTwo = referees(lastReferrer);
          while (dirRefereesTwo.hasMoreElements()) {
              var dirRefereeTwo = dirRefereesTwo.nextElement();
  
              //set this referee as our last item
              var newReferPath = referPath.slice();
              newReferPath.push(dirRefereeTwo);
              var referPathTest = getShortestReferPath(newReferPath, targetObj, maxDepth, depth + 1);
              if (referPathTest != null) return referPathTest;
          }
      }
  
      //return the current path
      return null;
  }
  </code></pre>
  
  <p>I run this bit of Javascript inside VisualVM's OQL query window.</p>
  
  <p>It is supposed to go through all referees of the referrerObj one by one up to a maximum depth, at which it should look to the next referee level if the target has not been found yet.</p>
  
  <p>For some reason however, the code seems to stop execution after following the possible paths of the first direct referee of the initial referrer passed.</p>
  
  <p>It looks like the second while loop is never completing. I am not getting any error code, but simply that nothing is returned.</p>
  
  <p>Does anyone have any clue why this could happen? If someone else could run this in VisualVM and report his/her findings, that'd be great.</p>
  
  <p>Thanks.</p>