• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

gc

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi everybody,

how to know which line will be garbage collection.
please give me any two examples,one is garbage collected.
onother one is not.please any one help me.
thanks
sri.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do a search on the word "garbage", you will find a lot of information.
http://www.javaranch.com/cgi-bin/ubb/search.cgi?action=intro&default=24
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello sri,
i ahve an example for you.
public class emp{ //line 1
public static void main(String[]s){ //line 2
emp e1,e2,e3,e4,e5; //line 3
e1=new emp(); //line 4
e2=new emp(21,"xyz"); //line 5
e3=e2; //line 6
e1=e3; //line 7
e3=new emp(); //line 8
e2=e3; //line 9
e4=e1; //line 10
e1=e2; //line 11
e5=e2; //line 12
}}
here object created at line 4 is garbage collected.
-------------------explanation----------------------------
1)at line 4 and 5 two different objects are created and there references are given by e1 amd e2.
2)in line 6, e3 is reffering same object that was reffered by e2.
3) at line 7 e2 is reffering the object which is initially reffered by e1.now note the object which was initially reffered by e2 is now only reffered by e3 not by e2.
4)in line 8 e1 is reffering the object which is reffered by e3 also.note the object which was initially reffered by e1 is now reffered only by e2 due to line 7.
5)at line 9 a new object is created and is reffered by e3.note the object which was earlier reffered by e3 is now only reffered by e1.
6)at line 10 e2 is reffering the same object which is reffered by e3 at line 9.
please note here that the object created at line 4 has no reference because the latest reference e2 is now reffering another object. hence the object that created at line 4is garbage collected.
for rest of the lines i am not giving explanations.
if you have any doubt please fill free to write me.
thanks
gautam
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Gautam,
Can u explain with some simple example, i guess u're confusing in line 7 and line 8 . i really appreciate if u come out with some simple ex. which will be helpful to us.
Thanks
rajan.
 
sanjay gautam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello ranjan.
a very simple ex:-
public class Name{
public static void main(String[])S){
Name x,y,z,p;
x=new Name (); //line 1
y=new Name("gautam"); //line2
z=new Name("gautam",24);//line3
p=x;//line4
x=y;//line5
p=z; //line6
}
}
here object created at line 1 is garbage collected.
explanation:--
1) at line 1,2 and 3 three different objects are created and referenced by x,y,z respectively.
2)on line 4,p is reffering same object which is reffered by x it means object created at line 1 has two references.
3)now at line 5, x is reffering the object which is earlier reffered by y.it means object created at line 2 has two references and object created at line 1 has only one reference i.e. p only.
4)at line 6 reference p is reffering the same object whih is reffered by z i.e. created at line 3.now please note that the object created at line 1 has no refernce bcoz now p is reffering some other object.
that's why object created at line 1 ig garbage collected.
hoping this is easy.please reply
gautam
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Gautam.
Thanx
Shallender
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic