• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

GarbageCollector

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{

public static void main(String []a)
{

String disp = new String("dinesh");
String show = new String("alok");

disp= show;
show=disp;

System.out.println(r);
System.out.println(p);
}

}

How many objects are garbage collected?

Please javaranchers help me out in understanding this concept...

Thanks In advance
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many do you think is eligible for garbage collection?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put your code between the code tags. Even for simple code it makes things easier.

What does 'r' and 'p' do ?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one object (dinesh), by the way what is "r" and "p"?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guys,

I have corrected the code

When i execute the prog i see the output as "alok alok". I am not getting how it is printing two times. Can you please explain in brief please

Also you told that one object is eligible for gc . which is that object?
According to me it should be disp. But disp is eligible i am not able to understand.
Correct me if i am wrong.

Ranchers it would be great if you could explain above scenario in simple terms. Please......

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this code is more about objects and reference variables than it is about garbage collection, and I'll let ranchers talk about that. But, in case this conversation moves too far forward, remember that on the real exam, GC questions will never use objects of type String.
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable so the two string objects on the heap dines and alok are not eligible for garbace collection until the program ends.

The two references disp and show live on the stack and will be removed when the main method is done.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,

The two references disp and show live on the stack and will be removed when the main method is done


I am not able to get this.

As far as i know objects references live on heap and methods and local variable on stack.

Can you please help me to understand this sentence which you have written. Please
 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since disp and show are declared inside a method they are considered as local variable.References of disp & show live on stack & objects associated with them is created on heap. As there visibility is only till main method,therefore after completion of main method they are eligible for garbage collection.

Also note that instance variables & objects live on heap.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
disp and show are local reference variables. All rules for local variables are applied for local reference variables.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic