Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on garbage collection?

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:-when the program is execute how many objects will be collected by the garbage collection process.




Ans-1
but , I get it why it is 1,but i want access that ungarbageble object and how to access it.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the "test" constant, which you can access as that - "test".
That is also the object returned by s1.intern(), even though s1 is a completely different object.
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
___________________________________________________________________________
public class Test1{public static void main(String[] args){String s="test";String s1 = new String("test"); System.out.println(intern());s=s1=null;}}

Ans-1
but , I get it why it is 1,but i want access that ungarbageble object and how to access it
__________________________________________________________________________

In my opinion ,in the above mentioned code you have created one object having two refrences.However you have also using intern,though calling intern() you need to specify the object either s or s1 ( in our case).
Let us suppose ,we called s.intern() . it means the "test" object would not be created and will only refreced to s1 from the private string pool of a class.( i belive you must have idea about string pool).
Assigning null to any refrence does not mean it will be garbage collected ,it just mean that " it eligible for garbage collection".

Hope it above justification would be helpful.
________________________
Mintoo
SCJP 1.4
__________________________
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mintoo,but I think the following program creates two objects:-



Because it returns false,so they refer to two different objects.

According to the following link, i can access that ungarbagable object by intern() method, can i access this object after nulling the two references.
web page
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for String pool, that should tell you all you need to know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic