Agent and sizing

Remi Forax forax at
Fri Oct 13 06:11:51 UTC 2017

Hi Henry,
technically, scanning all the fields of a class is warning free,
but to compute the size, you need the actual value of the fields, so you need to extract the value of a field, even if it's a private field, and this get you a lot of warning (or error).

----- Mail original -----
> De: "Henri Tremblay" <henri.tremblay at>
> À: "Stephen Felts" <stephen.felts at>
> Cc: "jdk9-dev" <jdk9-dev at>
> Envoyé: Vendredi 13 Octobre 2017 05:54:13
> Objet: Re: Agent and sizing

> Thanks a lot to you both! That helps a lot.
> And so, for the other question: Is there a way to walk through fields on
> classes the can be anywhere without tons of warnings?
> On 12 October 2017 at 15:55, Stephen Felts <stephen.felts at> wrote:
>> One way is to set the environment variable
>> JDK_JAVA_OPTIONS="-Djdk.attach.allowAttachSelf=true"
>> to turn off the self-attach error.
>> Another way is to use ProcessBuilder to create another process and have
>> the new process attach.  This follows all of the current rules.  Several
>> projects are using this approach.
>> -----Original Message-----
>> From: Henri Tremblay [mailto:henri.tremblay at]
>> Sent: Thursday, October 12, 2017 3:35 PM
>> To: jdk9-dev <jdk9-dev at>
>> Subject: Agent and sizing
>> Hi,
>> Yesterday I was playing with the sizeof <
>> >
>> library on Java 9. I was expecting bad things to happen. And I was right.
>> The purpose of this library is the give on heap occupation. We use it for
>> ehcache to know the size of the on heap cache and it is used by some other
>> frameworks as well.
>> I noticed two things. First, we load dynamically an agent in the current
>> JVM. This is done by reflection so it will be something like:
>> Class vm = Class.forName("");
>> Method attach = vm.getMethod("attach");
>> String name = ManagementFactory.getRuntimeMXBean().getName();
>> attach.invoke(null, name.substring(0, name.substring(0,
>> name.indexof('@')));
>> It works fine on Java 8 but fails on Java 9 with IOException: Can not
>> attach to current VM.
>> *How can I fix it?* (we then use the Instrumentation to do a getObjectSize.
>> It is one of the ways to make it work).
>> My other question is about the other sizeof implementations. One is using
>> reflection to go deep into objects until reaching the primitives. That will
>> make the JVM scream warnings all over the place I think.
>> Another is using Unsafe (objectFieldOffset, arrayBaseOffset,
>> arrayIndexScale) to calculate the size. I think this one should not cause
>> too much warnings.
>> *So, I guess there is not much I can do for the ReflectionSizeOf appart
>> from adding tons of JVM params?*
>> Thanks a lot,
>> Henri

More information about the jdk9-dev mailing list