aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stack Variables and GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stack Variables and GC" Watch "Stack Variables and GC" New topic
Author

Stack Variables and GC

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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??


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.

SCJP 6
Mitesh Soni
Greenhorn

Joined: Aug 17, 2007
Posts: 18
Hi,

All objects are stored in a Heap and not stack i guess.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Mitesh Soni wrote:Hi,

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


And you guess it right


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Here, String s in local reference variable, it will be stored in stack.
While new String("abc"); will be stored in heap.
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

OK ..got it .

Anything with NEW keyword => heap
Anything else locally declared =>stack
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Duran now tell me about this code, what will be store where, this is your test:

Mitesh Soni
Greenhorn

Joined: Aug 17, 2007
Posts: 18
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 :-(
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 165

check this also
http://www.javaranch.com/journal/200409/Journal200409.jsp
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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.


My Blog SCJP 5 SCWCD 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ok, I am feeling, if I use Object there than also it is a false statement, as Objects are always created on the heap.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Yes Punit, you are right in that case. Objects are created on heap. As mentioned earlier stack frames has only the references to them.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack Variables and GC