javap, jps, jmap, and jvisualvm tutorial – analyzing the heap histogram

Q1. Is there anything wrong with the code snippet shown below?

A1. The above code creates too many “Integer” objects due to auto boxing.

Q2. How do you know that the above auto unboxing and boxing code were executed?
A2. Because of “javap“. “SumNumbers” is the class file in the bin directory.

You get:

9: to 31: is the for loop.

13: is Integer.intValue, auto unboxing.

18: is Integer.valueOf, auto boxing.

Q3. How do you know that the above auto boxing and unboxing have created too many Integer objects?
A3. Firstly with “jmap“.

Step 1: Run SumNumbers, with

Step 2: jps to find the process number of “SumNumbers”.

9104 is the pid (process id)

Step 3:jmap histo” to see the instances of objects and byte sizes.

Step 4: Inspect “mem.txt” file.

Instances and bytes if “Integer” keeps increasing until garbage Collected.

After some time repeating the step 3.

wow, it has increased.

Understanding jmap histo output

Q4. Is it possible to represent this graphically?
A4. Yes, using jmap heap & jvisualvm

Open “jvisualvm”

Within jvisualvm top menu

Here is the visual representation of objects in the heap

jvisualvm histogram

jvisualvm histogram


Java FAQs to Fast-track & Go places

Java Interview Q&As

Top