Forums Register Login

Stack Variables and GC

+Pie Number of slices to send: Send
Hi...

I read that GC doesn't deal with stack(local) variables..How can this be true?

For example ,doesn't that mean that- any objects declared locally in main can't be GC'ed??
+Pie Number of slices to send: Send
If you do not use new keyword, variables will not be created in the heap.


Here i and j are not dealt by GC. They get destroyed with stack frames.
+Pie Number of slices to send: Send
Hi,

All objects are stored in a Heap and not stack i guess.
+Pie Number of slices to send: Send
 

Mitesh Soni wrote:Hi,

All objects are stored in a Heap and not stack i guess.



And you guess it right
+Pie Number of slices to send: Send


Here, String s in local reference variable, it will be stored in stack.
While new String("abc"); will be stored in heap.
+Pie Number of slices to send: Send
OK ..got it .

Anything with NEW keyword => heap
Anything else locally declared =>stack
+Pie Number of slices to send: Send
Duran now tell me about this code, what will be store where, this is your test:

+Pie Number of slices to send: Send
String s = "abc";

"abc" will go in the pool and s will refer to it.

String s = new String("abc");

Java will create a new String object
in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.

Punit From this I doubt that Pooll contains only references as per that tutorial :-(
+Pie Number of slices to send: Send
A string literal is always of type String (§4.3.3. A string literal always refers to the same instance (§4.3.1) of class String.
JLS String Literal

This means String Literals are just references to the instances of class String. It clears that String literal pool contains literals means only references to String instance. So if String s="abc"; is called "abc" will be created as String Instance on the heap and its literal means its reference will be stored in the pool.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
All objects are created in Heap. If you are creating any Objects in method like


Then its reference will be present in stack and Object will be in heap. Once the Method finishes its execution the whole stack frame is blown up and thus now there are no more references to the locally created Objects. So all the Objects which were created becomes eligible for GC. In case you return the reference of any Object then you can even access it after the completion of the method.

About the primitive types they live in the stack frame. So as all know there life time is only till the execution of the method.

Hope this makes things clear.
+Pie Number of slices to send: Send
 

Punit Singh wrote:If you do not use new keyword, variables will not be created in the heap.


Watch out that you use the language exactly right: variables are not created on the heap, you meant objects instead of variables here. A variable of a non-primitive type is a reference to an object in Java.
+Pie Number of slices to send: Send
Ok, I am feeling, if I use Object there than also it is a false statement, as Objects are always created on the heap.
+Pie Number of slices to send: Send
Yes Punit, you are right in that case. Objects are created on heap. As mentioned earlier stack frames has only the references to them.
+Pie Number of slices to send: Send
Corey's article is fantastic, but I'd like to add one statement:

On the real exam GC questions NEVER use objects of type String - so understanding the literal pool is a good thing but in terms of GC it's NOT on the SCJP exam.


hth,

Bert
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1407 times.
Similar Threads
Garbase Collection
gc from msterexam
Garbage collection question
doubt in garbage collection
stack vs heap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:10:59.