• Post Reply Bookmark Topic Watch Topic
  • New Topic

storage area for objects,variables and other entities in jvm  RSS feed

 
vaibhav vishal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does JVM allocate memory to entities while executing any program??
what is the memory architecture in JVM?

Like for example , in the below given statement:

Animal a = new Animal(); // Animal is a class

where is the reference "a" stored and the object created using heap??
similarly where the static variables , local variables and the class and method definitions are stored??

In respect of above what are new and old generations, what is Eden space???
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All local variables, including method parameters, go on the stack. Everything else--class definitions, object instances, static and non-static member variables, constant pools--is in the heap. To answer "What is Eden space?" Google is your friend.
 
vaibhav vishal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:All local variables, including method parameters, go on the stack. Everything else--class definitions, object instances, static and non-static member variables, constant pools--is in the heap. To answer "What is Eden space?" Google is your friend.



what if "Everything else--class definitions, object instances, static and non-static member variables, constant pools" is also local to some method??
in this case i think the object will go in heap but what about other entities???
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vaibhav vishal wrote:
Jeff Verdegan wrote:All local variables, including method parameters, go on the stack. Everything else--class definitions, object instances, static and non-static member variables, constant pools--is in the heap. To answer "What is Eden space?" Google is your friend.



what if "Everything else--class definitions, object instances, static and non-static member variables, constant pools" is also local to some method??


Like I said in my first response: Everything except local variables is on the heap.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects created inside methods, to which a local variable in the method refers, are also normally on the heap (while the variable itself is on the stack).

There is, however, an optimization called escape analysis. With this optimization, the JVM might allocate the memory for the object itself on the stack instead of on the heap. This optimization can be done for objects that do not escape the method, that is, objects that are only used inside the method, where no reference to the object is passed outside the method. The advantage to putting an object on the heap instead of the stack is that deallocating the memory for the object is essentially free: it's automatically discarded at the moment the method returns, when the stack pointer is restored - the garbage collector doesn't have to deal with it at all. The disadvantage is that it costs stack space, so your might get a StackOverflowError quicker (probably the JVM also checks the size of objects and doesn't try to put it on the stack if it's too large). The name "escape analysis" for this optimization refers to what the JVM needs to do: it needs to analyze if an object escapes from the method to determine if this optimization can be performed.

Escape analysis was introduced in one of the Oracle Java 6 updates (I think update 14).

Note that this is an advanced subject about the internal workings of the JVM - normally you don't need to be aware of escape analysis when you're programming in Java.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The advantage to putting an object on the heap instead of the stack is that deallocating the memory for the object is essentially free: it's automatically discarded at the moment the method returns,


Or, rather, the advantage to putting in on the stack instead of the heap.

Escape analysis was introduced in one of the Oracle Java 6 updates (I think update 14).


Really??? I thought it was considered for Java 7 but shelved.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vaibhav vishal wrote:In respect of above what are new and old generations, what is Eden space?

Do you have any particular reason for needing to know this information? As Jesper said, this is advanced stuff, and unless you're planning on writing a "new improved" Java garbage collector, the chances are you'll never need it. I've managed to get through 10 years without needing to know it in detail, and it certainly won't make you a better Java programmer...indeed, it may well distract you.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Escape analysis was introduced in one of the Oracle Java 6 updates (I think update 14).


Really??? I thought it was considered for Java 7 but shelved.

It was optional in 14, disabled in 18 and reintroduced in 21.
 
vaibhav vishal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
vaibhav vishal wrote:In respect of above what are new and old generations, what is Eden space?

Do you have any particular reason for needing to know this information? As Jesper said, this is advanced stuff, and unless you're planning on writing a "new improved" Java garbage collector, the chances are you'll never need it. I've managed to get through 10 years without needing to know it in detail, and it certainly won't make you a better Java programmer...indeed, it may well distract you.

Winston



actually i read these terms somewhere and they were very new to me...so just out of curiosity...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an interesting article which explains escape analysis in more detail: Java theory and practice: Urban performance legends, revisited (it's from 2005, so a bit old, but still relevant).

The Wikipedia article Java performance lists some of the optimization techniques that are used in the JVM.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!