I have coded a simple program to understand how VisualVM works. Here is the full code:
package memorygames;
public class MemoryGames
{
static class A {
private int i;
private String s;
A(int i, String s)
{
this.i = i;
this.s = s;
}
public int getI()
{
return i;
}
public String getS()
{
return s;
}
}
public static void main(String[] args) {
A a = new A(23, "hello");
while(true) {
System.out.println(a.getS());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
Now I make a HeapDump of my application in VisualVM. I see the instance of my 'A' class, but the s variable is empty! Its value equal to null. 'i' variable holds the correct value of 23.
Why this happens?
UPDATE: May be I am misusing the VisualVM? Here is my screenshot: http://oi42.tinypic.com/2091qfl.jpg
UPDATE: Not sure, but probably this was a result of some memory optimization. I have added
while(true) {
a.setS(new String("a-setter " + Math.random()));
(...)
}
... and now I can see the string value in the variable. I wonder do other people could really see the results in the Heap different from mine.