This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many object created ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many object created ?" Watch "how many object created ?" New topic
Author

how many object created ?

gaurav gupta sitm
Greenhorn

Joined: Jan 12, 2011
Posts: 22


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

Joined: Mar 07, 2010
Posts: 438

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

    SCJP 6 [86%] June 30th, 2010
    OCPWCD [84%] March 26th, 2013
    If you find any post useful, click the "plus one" sign on the right
    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3605
        
      14

    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

    Joined: Jan 09, 2011
    Posts: 173

    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

    Joined: Aug 31, 2010
    Posts: 394

    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.


    OCPJP 6.
    In Your Pursuit Towards Certification, NEVER Give Up.
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 438

    @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

    Joined: Jan 09, 2011
    Posts: 173

    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 ..


     
     
    subject: how many object created ?
     
    Similar Threads
    How many Objects are created ?
    Object creation??
    RHE Qs
    Concatenate->Print OR System.out.print() ???
    how many objects are created?