java heap analysis with oql: Count unique strings

前端 未结 6 1012
自闭症患者
自闭症患者 2020-12-29 09:11

Im doing a memory analysis of an existing java software. Is there a sql \'group by\' equivalent in oql to see the count of objects with same values but different instances.<

6条回答
  •  难免孤独
    2020-12-29 09:31

    A far more efficient query:

    var countByValue = {};
    
    // Scroll the strings
    heap.forEachObject(
      function(strObject) {
        var key = strObject.toString();
        var count = countByValue[key];
        countByValue[key] = count ? count + 1 : 1;
      },
      "java.lang.String",
      false
    );
    
    // Transform the map into array
    var mapEntries = [];
    for (var i = 0, keys = Object.keys(countByValue), total = keys.length; i < total; i++) {
      mapEntries.push({
        count : countByValue[keys[i]],
        string : keys[i]
      });
    }
    
    // Sort the counts
    sort(mapEntries, 'rhs.count - lhs.count');
    

提交回复
热议问题