• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many object created ?

 
gaurav gupta sitm
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How many objects are created ?
String[] args is counted or not ?
is there any method in java by which we can get "How many objects are created in current program" .........
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my opinion so I am not sure about the answer

I did a simple not null test and found that args object is also created
this is of type Array

and in the following lines
according to me
following happens
  • s1 object will refer to a new StringBuffer object and it has String literal "abc", this will also be created in String constant pool memory
  • s2 variable will just refer to the object referred by s1
  • and again, s3 will again create a new StringBuffer Object but the value "abc" will be used from the constant pool memory


  • so according to me
    total 4 objects will get created
    two StringBuffer, one String literal and the args Array object


    this is my opinion
    let the experts comment over it
     
    Stephan van Hulst
    Bartender
    Pie
    Posts: 5601
    55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm with Prasad on this one.

    Of course, it's a bit of an ambiguous question, because we don't count the objects that are created inside the String and the StringBuffer instances.
     
    Hareendra Reddy
    Ranch Hand
    Posts: 173
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello all,

    What about the exception objects??


    In the above code does exception objects will be garbage collected??

    [edit]
    i mean how many objects are to be garbage collected after execution the method??
     
    Ikpefua Jacob-Obinyan
    Ranch Hand
    Posts: 394
    Eclipse IDE Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hareendra Reddy wrote:Hello all,

    What about the exception objects??



    In the above code does exception objects will be garbage collected??

    Hello Hareendra I want to give you my understanding of the above code.

    1. An object is eligible for garbage collection when NO LIVE threads can access it. And that means NOT having a REACHABLE reference variable that refers to the object.
    2. According to the execution of the above program, an exception is thrown in line 5 that is caught in line 6 and runs the codes in line 7 and 9 respectively.
    3. nfe AND i are REACHABLE reference variables -(I THINK SO)- which means the objects they refer to CANNOT be garbage collected.

    4. s does NOT refer to any object.

    5. The answer to your question is NO the exception object that nfe refers to will NOT be garbage collected.

    PLEASE take note that I am studying java just like you, I might have made a mistake in my explanation or not.
    Thats up to the experts that mediate in this forum to look out for, I hope you find my analysis usefull thanks.
     
    Prasad Kharkar
    Ranch Hand
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Ikpefua

    I want to appreciate you that you are very clear and nice way to explain also
    and don't hesitate to ask even the questions which you seem silly
    no question is silly at all


    @Harindra, objects eligible for garabage collection cannot be determined here according to me
    as this method testing will be called by passing some value
    and if that value is referred by a variable, then s= null in this method won't make it eligible for garbage collection

    for that, it will be good if complete code is provided

    happy to help
     
    Hareendra Reddy
    Ranch Hand
    Posts: 173
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Prasad and Ikpefua...

    I just mean to know whether exceptions are also garbage collected when they are caught like any other objects or not !!
    In the above code the scope of the variable is up to catch block i suppose ..


     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic