File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stack Variables and GC

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mitesh Soni
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

All objects are stored in a Heap and not stack i guess.
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitesh Soni wrote:Hi,

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


And you guess it right
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK ..got it .

Anything with NEW keyword => heap
Anything else locally declared =>stack
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran now tell me about this code, what will be store where, this is your test:

 
Mitesh Soni
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this also
http://www.javaranch.com/journal/200409/Journal200409.jsp
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic