Strong reference shallow size via MemorySnapshot API?

Questions about YourKit Java Profiler
Post Reply
camlow
Posts: 2
Joined: Thu Jun 16, 2016 9:23 pm

Strong reference shallow size via MemorySnapshot API?

Post by camlow »

Hello.

When I open a memory snapshot via the YourKit Java Profiler GUI, I can see the cumulative "shallow size" for all objects that are strong reachable via a GC root.

For example, I might see:
Objects: 7,935,461 / shallow size: 782 MB / retained size: 782 MB Strong reachable among them: 2,090,931 (26%) / shallow size 93 MB (11%) / retained size: 233 MB (29%)
... where "93 MB" is the cumulative "shallow size" for all objects that are strong reachable via a GC root.

Is it possible to get this same number -- the "93 MB" number from the example above -- from a memory snapshot programmatically, via the com.yourkit.api.MemorySnapshot (or some other) API? I believe the MemorySnapshot.getShallowSize() method uses XML query syntax like the examples on the "set description language" page (https://www.yourkit.com/docs/java/help/language.jsp). I haven't found anything on that page, though, which mentions anything specific about filtering the list of objects selected to only those which are reachable from GC roots via strong references, like what is shown in the Profiler GUI.

A query like "<objects class=\"*\">" would seem to just give all available objects. A query like "<retained-objects><roots/></retained-objects>" appears to consider the cumulative set of objects that are reachable from GC roots but this also appears to include objects that aren't included in the "strong reachable" filter that the GUI provides.

Thanks much for any pointers / advice you might be able to provide!
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Re: Strong reference shallow size via MemorySnapshot API?

Post by Anton Katilin »

Hi,

You can approximate this with the following:

Code: Select all

<reachable-objects>
      <from>
        <roots/>
      </from>

      <object-filter>
        <objects class="*"/>
      </object-filter>

      <field-filter>
        <class name="java.lang.ref.Reference">
          <forbidden field="referent"/>
        </class>
      </field-filter> 	
</reachable-objects>
This is not exactly the same as the strongly reachable objects as seen in the UI. The difference is that the above description also includes the finalization queue elements which are instances of java.lang.ref.Finalizer. Note this does not include the objects pending finalization themselves, just the finalization queue itself, i.e. "shallow" list. Nevertheless, this should be a minor fraction of objects and perhaps you can simply ignore it.

If you want, we can consider adding a dedicated tag for objects with given reachability scope in the next EAP build.

Best regards,
Anton
camlow
Posts: 2
Joined: Thu Jun 16, 2016 9:23 pm

Re: Strong reference shallow size via MemorySnapshot API?

Post by camlow »

Hi Anton,

Thanks, that query looks great. With one slight change to filter out the 'java.lang.ref.Finalizer' objects, it seems to be giving me the same object set as what I see by just clicking on the "Strong References" link - at least for the first few snapshots I've looked at. Here's what I'm using:

Code: Select all

<reachable-objects>
           <from>
             <roots/>
           </from>
           <object-filter>
             <not>
               <objects class="java.lang.ref.Finalizer"/>
             </not>
           </object-filter>
           <field-filter>
             <class name="java.lang.ref.Reference">
               <forbidden field="referent"/>
             </class>
           </field-filter>
         </reachable-objects>
If there would be a way to add a dedicated object tag for reachability scope into the query language, that would be great. Thanks again!
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Re: Strong reference shallow size via MemorySnapshot API?

Post by Anton Katilin »

Thank you for sharing the improved description.

In the next EAP build we'll add new tags <strong-reachable/>, <weak-soft-reachable/>, <pending-finalization/>, <unreachable/> that correspond to particular reachability scopes.
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Re: Strong reference shallow size via MemorySnapshot API?

Post by Anton Katilin »

EAP build 12 with the new tags is available for download.
Post Reply