转载

【深入浅出-JVM】(58):OQL

OQL (Object Query Language)类似于 SQL 的查询语言,可以方便在堆中进行对象的查找和 筛选。

语法

select [from[instanceof] ][where ]

select * from com.XX.CacheManager

例子(MAT 版本)

1.查询string

SELECT * FROM java.lang.String

2.正则查询 java.lang

SELECT * FROM "java.lang..*"

3.查询类地址0x79571fb40

SELECT * FROM 0x79571fb40

4.属性访问器

SELECT * FROM ${snapshot}.getClasses()

5.条件查询

SELECT * FROM java.lang.String s WHERE toString(s) LIKE ".*day"

6.查询char数组长度大于 10 的

select * from char[] s where s.@length > 10

7.查询长度大于 15 且深堆大于 1000 字节的所有 Vector 对象

select * from java.util.Vector v where v.elementData.@length > 15 AND v.@retainedHeapSize > 1000

8.显示Vector内部数组长度

SELECT v.elementData.@length FROM java.util.Vector v

9.显示int数组索引下表为 2 的数据内容

SELECT s.getValueAt(2) FROM int[] s WHERE (s.@length > 2)

10.显示堆中所有类型

select * from ${snapshot}.getClasses()

11.显示长度为 4 且值不为空的string对象内容

SELECT toString(s) FROM java.lang.String s WHERE ((s.value.@length = 4) and (s.value != null))

12.查看保留集

SELECT AS RETAINED SET * from com.mousycoder.mycode.thinking_in_jvm.Student

13.查询 string 实例

select distinct objects classof(s) from java.lang.String s

14.查询包下的实例

select * from "com/.mousycoder/.mycode/.thinking_in_jvm/..*"

15.查找子类

SELECT * FROM INSTANCEOF java.util.AbstractCollection

16.查看类的属性

SELECT toString(f.path.value) FROM java.io.File f

17.查看对象地址

SELECT s.toString(), s.@objectId, s.@objectAddress FROM java.lang.String s

例子(Visual VM版本)

  1. 筛选长度大于 100 的字符串

    select s from java.lang.String s where s.value.length >=100

    2.查找 classloader的子类

    select cl from instanceof java.lang.ClassLoader cl

    3.查找父类

    select heap.findClass("java.util.Vector").superclasses()

    4.输出引用链

    select heap.livepaths(s) from java.lang.String s where s.toString() == '56'

    5.访问对象属性

    select heap.findClass("com.mousycoder.mycode.thinking_in_jvm.TraceStudent").webpages

    6.查看可达对象

    select {r:toHtml(reachables(s)),url:s.url.toString()} from com.mousycoder.mycode.thinking_in_jvm.WebPage s

    7.长度为 2 至少被 2 个对象引用的字符串

    select s.toString() from java.lang.String s where (s.value != null && s.value.length == 2 && count(referrers(s)) >= 2)

    8.查找直接引用对象

    select referees(s) from com.mousycoder.mycode.thinking_in_jvm.Student s

    9.Vector的浅堆

    select {size:sizeof(o),Object:0} from java.util.Vector o

    10.Vector的深堆

    select {size:sizeof(o),rsize:rsizeof(o)} from java.util.Vector o

    11.通过File 类打开的文件

    select {size:sizeof(o),rsize:rsizeof(o)} from java.util.Vector o

    12.查找长度最长的 5 个字符串并显示对象和长度

    select map (top(filter(heap.objects('java.lang.String'),'it.value !=null'),'rhs.value.length -lhs.value.length',5),'{length: it.value.length,obj:it}')

    13.查看string字符串一共多少个不重复的元素

    select count(unique(map(heap.objects('java.lang.String'),'it.value')))

    14.统计脚本

    var sessions = toArray(heap.objects("java.lang.xxx"))
    var count = sessions.length;
    var createtimes = new Array();
    for(var i=0;i<count;i++){
      createtimes[i] = sessions[i].createTime;
    }
    createtimes.sort();
    var min = createtimes[0]/1000;
    var max = createtimes[count-1]/1000;
    
原文  http://mousycoder.com/thinking-in-jvm/58/
正文到此结束
Loading...