Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand MAT's support for OQL in java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to understand MAT's support for OQL in java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

MAT supports OQL (Object Query Language), a query language similar to SQL. OQL uses the SQL-like syntax to find and filter objects in the heap.

1. Select clause

In MAT, the format of the Select clause is basically the same as that of SQL and is used to specify the columns to display. You can use "*" in the Select clause to view the reference instance of the result object (equivalent to outgoing references).

Select * from java.util.Vector v

The output of the above query is shown in the figure, in which each record in the result set can be expanded to view its own reference object.

OQL can also specify the properties of the object for output. The following example outputs the internal array of all Vector objects, as shown in the figure. Using the "OBJECTS" keyword, items in the returned result set can be displayed as objects.

SELECT OBJECTS v.elementData FROM java.util.Vector v

In the Select clause, the reserved set of the resulting object can be obtained using the "AS RETAINED SET" keyword. The following example gets the reserved set of the jvm.chapter07.Student object.

SELECT AS RETAINED SET * FROM jvm.chapter07.Student

The "DISTINCT" keyword is used to remove duplicate objects from the result set. There is only one "class java.lang.String" record in the output of the following example. If there is no "DISTINCT", the query will output its corresponding Class information for each String instance.

SELECT DISTINCT OBJECTS classof (s) FROM java.lang.String s

2. From clause

The From clause is used to specify the query scope, which can specify a class name, a regular expression, or an object address.

The following example uses the From clause, specifies the class name to search, and outputs all java.lang.String instances.

SELECT * FROM java.lang.String s

The following example uses regular expressions to limit the search and output instances of all classes under all java.lang packages, as shown in the figure.

SELECT * FROM "jvm\ .chapter07\.. *"

You can also search directly using the address of the class. The advantage of using the address of a class is that you can distinguish between the same type loaded by different ClassLoader. In the following example, "0x37a014d8" is the address of the class.

Select * from 0x37a014d8

There are several ways to get the address of a class, and in MAT, one of the easiest methods is shown in the figure.

In the From clause, you can also use the "INSTANCEOF" keyword to return all subclass instances of the specified class. The query in the following example returns all abstract collection instances in the current heap snapshot, including java.util.Vector, java.util.ArrayList, java.util.HashSet, and so on.

SELECT * FROM INSTANCEOF java.util.AbstractCollection

The "OBJECTS" keyword can also be used in the From clause. After using the "OBJECTS" keyword, the query that should have returned an instance of the class will return the information about the class.

SELECT * FROM OBJECTS java.lang.String

The returned results of the above query are shown in the figure. It returns only one record that represents information about the class of java.lang.String.

If you do not use the "OBJECTS" keyword, this query returns all java.lang.String instances:

The "OBJECTS" keyword is also supported for use with regular expressions. The following query returns all classes that satisfy a given regular expression, with the results shown in the figure.

SELECT * FROM OBJECTS "jvm\ .chapter07\.. *"

Note: using the OBJECTS keyword in the From clause returns class information that matches the criteria, not instance information. This is completely different from the OBJECTS keyword in the Select clause.

3. Where clause

The Where clause is used to specify the query criteria for OQL. The OQL query will return only objects that meet the criteria specified in the Where clause. The format of the Where clause is very similar to that of traditional SQL.

The following example returns an char array of length greater than 10.

SELECT * FROM char [] s WHERE s.@length > 10

The following example returns all strings that contain the "java" substring, using the "LIKE" operator, and the operation parameter of the "LIKE" operator is a regular expression.

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

The following example returns all strings whose value field is not null, using the "=" operator.

SELECT * FROM java.lang.String s where s.valuevalues null

The Where clause supports AND and OR operations for multiple conditions. The following example returns all Vector objects with an array length greater than 15 and a deep heap greater than 1000 bytes.

SELECT * FROM java.util.Vector v WHERE v.elementData.@length > 15 AND v.@retainedHeapSize > 10004 built-in objects and methods

In OQL, you can access the properties of objects in the heap or the properties of proxy objects in the heap. When accessing the properties of objects in the heap, the format is as follows:

[. ]. .

Where alias is the object name.

The following example accesses the path property of the java.io.File object and further accesses the value property of path.

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

The result of the above query is shown in the figure.

The properties of these heap objects are the same as the Java objects and have the same results as the Java objects.

In order to quickly obtain the additional properties of objects in the heap (such as heap size occupied by objects, object address, etc.), MAT establishes a corresponding proxy object for each meta-type of objects in the heap to enhance the original object function. When accessing the properties of a proxy object, use the following format:

[. ] @

Where alias is the object name and attribute is the attribute name.

The following example shows the contents, objectid, and objectAddress of the String object.

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

The following example shows the object ID of the File object, the object address, the type of the proxy object, the type of the class, the shallow heap size of the object, and the display name of the object.

SELECT f.@objectId, f.@objectAddress, f.@class, f.@clazz, f.@usedHeapSize, f.@displayName FROM java.io.File f

The following example shows the length of the array inside the java.util.Vector.

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

The following table collates the basic properties of the MAT proxy object.

Object type interface function base object IObejctobjectId object ID base object address base object IObejctclass proxy object type base object IObejctclazz class type base object IObejctusedHeapSize shallow heap size base object IObejctretainedHeapSize deep heap base object IObejctdisplayName display name Class object IClassclassLoaderIdClassLoad ID array IArraylength array length meta type array IPrimitiveArrayvalueArray array content array IObjectArrayreferenceArray array content

In addition to using the properties of a proxy object, the method of a proxy object can also be used in OQL in the following format:

[. ] @ ([,])

The following example shows the data content with index 2 in the int array.

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

The following example shows an object with index 2 in an array of objects.

SELECT OBJECTS s.@referenceArray.get (2) FROM java.lang.Object [] s WHERE (s.@length > 2)

The following example shows all the types in the current heap.

Select * from ${snapshot} .getClasses ()

The following example shows all the java.util.Vector objects and their subtypes, and its output is shown in the figure.

Select * from INSTANCEOF java.util.Vector

The following example shows whether the current object is an array.

SELECT c, classof (c). IsArrayType () FROM ${snapshot} .getClasses () c

The methods of the proxy object are organized as shown in the table.

Object description object name object method object method Global Snapshot ISnapshotgetClasses () Collection of all instances Global Snapshot ISnapshotgetClassesByName (String name, boolean includeSubClasses) Select a qualified instance class object IClasshasSuperClass () based on the name whether the superclass object IClassisArrayType () is an array base object IObjectgetObjectAddress () gets the object address meta-type array IPrimitiveArraygetValueAt (int index) gets the data meta-type array of the given index in the array The object array [] or Listget (int index) gets the data of a given index in the array

There are also some useful functions built into MAT's OQL, as shown in the table.

Built-in functions in table OQL

Function description: toHex (number) is converted to hexadecimal toString (object) to string dominators (object) to get the direct control object outbounds (object) gets the object referenced by the given object inbounds (object) gets the object classof (object) that references the given object, gets the class dominatorof (object) of the current object, and gets the direct controller of the given object.

The following example shows the contents of all strings of length 15 (heap exported by JDK 1.7).

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

The following example shows the direct domination object of all jvm.chapter07.Student objects. That is, the collection of objects that will be released after a given object is reclaimed.

SELECT objects dominators (s) FROM jvm.chapter07.Student s

The output of the above query is shown in the figure, showing that the Student object dominates three Vector objects.

The function dominatorof (), contrary to dominators (), gets the object that directly dominates the current object.

SELECT distinct objects dominatorof (s) FROM jvm.chapter07.Student s

The output of the above query is shown in the figure, which shows that all Student objects are directly controlled by the main thread.

Note: the function dominatorof () is the opposite of dominators (). Dominatorof () is used to get the object that directly dominates the current object, while dominators () is used to get the object that directly dominates the object.

The following example gets the object that references WebPage.

SELECT objects inbounds (w) FROM jvm.chapter07.WebPage w

The following example gets the type of all existing object instances in the jvm.chapter package in the heap snapshot, and its output is shown in the figure.

SELECT distinct objects classof (obj) FROM "jvm\ .chapter07\.. *" obj

On how to understand the MAT support for OQL in java is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report