• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory allocation  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its been so common question yet so confusing . where are the methods local variable instance variable object stored in java.
My answer.

Stack: used to store the local variables, method calls from the object , references to object, reference variable arguments

Heap : used to store all executable code ie method code, object instances.

Please correct me if i am going wrong .

I am also not aware of how method area works.


Another question : how many objects are stored in memory in following statement:

please verify with your kind corrections
.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Luke,

All the methods are stored in method area while loading the class. all the method and their local variables stored into the stack while executing and only memory allocated dynamically that is object created using new operator get memory in heap.

String s1= "hello";//i believe 1 object is created in the heap
// namely s1 point to the value hello in constant pool.
String s2= s1; //s2 a reference variable created in stack( // correct me here as am not sure of this)and s2 points to // object s1 in the heap .



In the first case no memory allocated in heap, it will store into constant pool area. as i said earlier only object created using new operator gets memory into the heap and in 2nd case reference of s1 will be stored in s2 which stored in stack.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something you don't actually need to know at all.

There is no such thing as a local instance variable. There are instance variables, which are not local, and they are stored as part of the object usually on the heap.
There are local variables which are not instance nor class variables. They live inside methods; they are put onto the stack when the method is running and their values are overwritten after the method completes.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the BCEL Manual, which is a bit old now, because that might help.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your reply, Tushar Goel, but it does not look correct to me.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
luke brown wrote:where are the methods local variable instance variable object stored in java.

I'm with Campbell on this one. Unless this is for the SCJP exam: DontSweatIt.

And TBH, I don't know why that exam (if it still does) makes such a meal of this stuff because it simply isn't important. It's an implementation detail which, unless you fancy writing a JVM of your own, you should never need to worry about.

And if, like me (and many others here), you came from a language like C or C++ where this stuff is important, try and "unlearn" it if you can. You'll be much happier for it. I have never once, in 12 years of writing Java, worried whether my objects, fields, parameters, etc go on the stack or the heap.

HIH

Winston

PS (@Luke and @Tushar): Please DontWriteLongLines. It makes the thread very hard to read. I've broken them up this time, but please read the link carefully. Thanks
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Campbell for confusion, What i meant was:

All the methods got memory in method area , all methods and their variable got memory in stack when they call and in heap only memory allocated when new object is created using 'new' operator.

and for

String s1= "hello"
String s2 = s1


What i meant was , string literal as s1 got memory in constant pool and s2 will store the reference of s1. There both referencing to same object.

String literal got memory in heap only when it created using new operator. like below

String s1 = new String("Hello");

In this case 2 objects will be created, one in constant pool and other one in heap.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
Tushar Goel wrote: . . . All the methods got memory in method area . . .
But you haven't said where the memory area is.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote: . . .

In this case 2 objects will be created, one in constant pool and other one in heap.
And where is this constant pool? In the class file or on the heap? You need a lot more explanation for people who are too inexperienced to know they don't need to know this
I was hoping the BCEL link earlier would answer all those questions.

Actually I have just looked at BCEL and it will only answer some of the questions.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And if, like me (and many others here), you came from a language like C or C++ where this stuff is important, try and "unlearn" it if you can. You'll be much happier for it.


Winston, you are so right. I have also never bothered to learn where those things are stored. Does it help to know how a bicycle is engineered? Many of us just know how to ride it.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you haven't said where the method area is.


method area , constant pool and class info are part of class area.. in JLS 2.5.4 and 5 explains method area and constant pool..

You need a lot more explanation for people who are too inexperienced to know they don't need to know this


hehehe.. ;-
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
But you haven't said where the method area is.


method area , constant pool and class info are part of class area.. in JLS 2.5.4 and 5 explains method area and constant pool..


Hmm... seems awfully early in the JLS to specify that sort of thing... <checks> Yup, section 2 of the JLS is about the Grammar used in the document and doesn't have a section 2.5. Mind sharing a link to what you actually got that information. Here is JLS section 2: http://docs.oracle.com/javase/specs/jls/se7/html/jls-2.html
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:Winston, you are so right. I have also never bothered to learn where those things are stored. Does it help to know how a bicycle is engineered? Many of us just know how to ride it.

Glad to see someone's got the message.

That's not to say it isn't important; just not to a Java programmer (or you, when you're programming in Java).

There are plenty of other things in the world - even the programming world - to be worrying about, and as a Java programmer, things like stacks and heaps are likely to distract you from your real task: which is to write good, clean code.

Winston
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mind sharing a link to what you actually got that information


Sorry its JVM specification.. here is the link http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html


Glad to see someone's got the message.


Winston, you are a wise man... Actually my teacher taught this so i remember this..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!