VisualVM 的 OQL 的一些例子

Visual VM的OQL语言是对HeapDump进行查询,类似于SQL的查询语言,它的基本语法如下:

select <JavaScript expression to select>
[ from [instanceof] <class name> <identifier>
[ where <JavaScript boolean expression to filter> ] ]

OQL由3个部分组成:select子句、from子句和where子句。select子句指定查询结果要显示的内容。from子句指定查询范围,可指定类名,如java.lang.String、char[]、[Ljava.io.File;(File数组)。where子句用于指定查询条件。

一些例子

字符串的长度大于等于 100 的实例

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

image

选取长度大于等于256的int数组。

select s
from int[] s
where s.length >= 256

显示所有文件对象的文件路径

select file.path.value.toString()
from java.io.File file

image

显示所有ClassLoader的类名

select classof(cl).name
from instanceof java.lang.ClassLoader cl

image

 

查找包含内容最多的List

这个应该是查找内存泄露的好语句

select map(top(heap.objects('java.util.ArrayList'), 'rhs.size - lhs.size', 5),"toHtml(it)+'='+it.size")

image

 

 

通过引用查询对象

select o from instanceof 0xd404d404 o

heap 对象

heap.findClass(class name) -- 找到类


select heap.findClass("java.lang.String").superclass

heap.findObject(object id) -- 找到对象


select heap.findObject("0xd404d404")

heap.classes -- 所有类的枚举

select heap.classes

heap.objects -- 所有对象的枚举

select heap.objects("java.lang.String")

heap.finalizables -- 等待垃圾收集的java对象的枚举

select heap.finalizables

heap.livepaths -- 某一对象存活路径

select heap.livepaths(s) from java.lang.String s

 

 

辨识对象的函数


classof(class name) -- 返回java对象的类对象


select classof(cl).name from instanceof java.lang.ClassLoader cl

identical(object1,object2) -- 返回是否两个对象是同一个实例


select identical(heap.findClass("java.lang.String").name, heap.findClass("java.lang.String").name)

objectid(object) -- 返回对象的id


select objectid(s) from java.lang.String s

reachables -- 返回可从对象可到达的对象


select reachables(p) from java.util.Properties p      -- 查询从Properties对象可到达的对象
select reachables(u, "java.net.URL.handler") from java.net.URL u -- 查询从URL对象可到达的对象,但不包括从URL.handler可到达的对象

referrers(object) -- 返回引用某一对象的对象


select referrers(s) from java.lang.String s where s.count > 100

referees(object) -- 返回某一对象引用的对象


select referees(s) from java.lang.String s where s.count > 100

refers(object1,object2) -- 返回是否第一个对象引用第二个对象


select refers(heap.findObject("0xd4d4d4d4"),heap.findObject("0xe4e4e4e4"))

root(object) -- 返回是否对象是根集的成员


select root(heap.findObject("0xd4d4d4d4"))

sizeof(object) -- 返回对象的大小


select sizeof(o) from [I o

toHtml(object) -- 返回对象的html格式


select "<b>" + toHtml(o) + "</b>" from java.lang.Object o

选择多值


select {name:t.name?t.name.toString():"null",thread:t} from instanceof java.lang.Thread t

 

 

数组、迭代器等函数


concat(enumeration1,enumeration2) -- 将数组或枚举进行连接


select concat(referrers(p),referrers(p)) from java.util.Properties p

contains(array, expression) -- 数组中元素是否满足某表达式


select p from java.util.Properties where contains(referres(p), "classof(it).name == 'java.lang.Class'")
返回由java.lang.Class引用的java.util.Properties对象
built-in变量
it -- 当前的迭代元素
index -- 当前迭代元素的索引
array -- 被迭代的数组

count(array, expression) -- 满足某一条件的元素的数量


select count(heap.classes(), "/java.io./(it.name)")

filter(array, expression) -- 过滤出满足某一条件的元素


select filter(heap.classes(), "/java.io./(it.name)")

length(array) -- 返回数组长度


select length(heap.classes())

map(array,expression) -- 根据表达式对数组中的元素进行转换映射


select map(heap.classes(),"index + '-->' + toHtml(it)")

max(array,expression) -- 最大值, min(array,expression)


select max(heap.objects("java.lang.String"),"lhs.count>rhs.count")
built-in变量
lhs -- 左边元素
rhs -- 右边元素

sort(array,expression) -- 排序


select sort(heap.objects('[C'),'sizeof(lhs)-sizeof(rhs)')

sum(array,expression) -- 求和


select sum(heap.objects('[C'),'sizeof(it)')

toArray(array) -- 返回数组


unique(array) -- 唯一化数组

 

 

 

参考:

http://book.51cto.com/art/201504/472224.htm

http://visualvm.java.net/oqlhelp.html 

http://www.iteye.com/topic/1125255

原文地址:https://www.cnblogs.com/ghj1976/p/5408295.html