Disposing of an object?

Discussion in 'Java' started by Stephen Marjoribanks, Mar 25, 2006.

  1. The title may be a little off topic actually but anyway... I have an
    application which every time an XML file is opened creates an instance
    of a class called XMLParser. This does not extend any other classes and
    it's function is to validate the XML file and retrieve all the required
    data from it and store it in arrays/hashmaps.

    If the user were to close the file, how long does the instance of
    XMLParser exist for? Is it disposed of automatically as long as no
    references to it exist or do I have to actually dispose of it manually?

    The reason I ask is that should the user open a second XML file after
    closing the first, another instance of XMLParser is created, with the
    new arrays holding the new data. When the other classes within the
    aplpication need to access the data would they not get confused between
    the first instance of XMLParser and the second? Bearing in mind they
    will be identical apart from the data stored within the arrays etc?

    So basically, how do I ensure that the first instance of XMLParser no
    longer exists or is no longer accessible when a second instance is created?

    Thanks

    Steve
    Stephen Marjoribanks, Mar 25, 2006
    #1
    1. Advertising

  2. Stephen Marjoribanks

    Roedy Green Guest

    On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
    <> wrote, quoted or indirectly quoted
    someone who said :

    >If the user were to close the file, how long does the instance of
    >XMLParser exist for? Is it disposed of automatically as long as no
    >references to it exist or do I have to actually dispose of it manually?


    Presuming any references to the old one are null or are replaced by
    references to a different object it will hang around until you run out
    of RAM. There will be a GC pass and all live objects will be scavenged
    leaving any dead ones behind and their space reclaimed. See
    http://mindprod.com/jgloss/garbagecollection.html

    It is considered more hip to create a new object even when the old one
    will do. Why? Presumably because in recycling there might be some
    slip in initialisation. Perhaps it is just an unconscious
    accommodation to consumer culture.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 25, 2006
    #2
    1. Advertising

  3. Stephen Marjoribanks

    Naveed Guest

    If there are no references to an object, that object is no longer
    accessible. It just stays in limbo until the garbage collector
    destroys it.
    Naveed, Mar 25, 2006
    #3
  4. Roedy Green wrote:
    > On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >> If the user were to close the file, how long does the instance of
    >> XMLParser exist for? Is it disposed of automatically as long as no
    >> references to it exist or do I have to actually dispose of it manually?

    >
    > Presuming any references to the old one are null or are replaced by
    > references to a different object it will hang around until you run out
    > of RAM. There will be a GC pass and all live objects will be scavenged
    > leaving any dead ones behind and their space reclaimed. See
    > http://mindprod.com/jgloss/garbagecollection.html


    Is this still the case if both the 'old' and 'new' objects are called
    the same though?
    For example in my code, when the user opens a file the code

    XMLParser parser = new XMLParser();

    executed. If the instance of XMLParser 'parser' is still hanging around
    (even if it is not in use anymore) and a new instance also called
    'parser' is created (by executing the line of code above again) how will
    it know which 'parser' to reference?

    >
    > It is considered more hip to create a new object even when the old one
    > will do. Why? Presumably because in recycling there might be some
    > slip in initialisation. Perhaps it is just an unconscious
    > accommodation to consumer culture.


    In my case, I guess I have done it for conveniences sake. Creating a new
    object just means that I don't have to write some more lines of code to
    clear out all of the arrays/hashtables within the object!

    Thanks

    Steve
    Stephen Marjoribanks, Mar 25, 2006
    #4
  5. Stephen Marjoribanks wrote:
    > Roedy Green wrote:

    ....
    >> Presuming any references to the old one are null or are replaced by
    >> references to a different object it will hang around until you run out
    >> of RAM. There will be a GC pass and all live objects will be scavenged
    >> leaving any dead ones behind and their space reclaimed. See
    >> http://mindprod.com/jgloss/garbagecollection.html

    >
    >
    > Is this still the case if both the 'old' and 'new' objects are called
    > the same though?
    > For example in my code, when the user opens a file the code
    >
    > XMLParser parser = new XMLParser();
    >
    > executed. If the instance of XMLParser 'parser' is still hanging around
    > (even if it is not in use anymore) and a new instance also called
    > 'parser' is created (by executing the line of code above again) how will
    > it know which 'parser' to reference?


    This may be clearer with a shift in mental model of what the line of
    code says.

    It does not mean "Create an XMLParser and name it 'parser'". It means
    "Declare a reference variable, scope from here to the end of the current
    block, named 'parser' and initialized with a pointer to a new XMLParser".

    In Java, objects don't have names, so it does not make sense to think of
    "the instance of XMLParser 'parser'". A reference variable does have a
    name and is either null or a pointer to some object, so while 'parser'
    remains in scope, it does make sense to think of "the XMLParser that
    variable 'parser' currently references".

    Patricia
    Patricia Shanahan, Mar 25, 2006
    #5
  6. "Roedy Green" <> wrote in
    message news:p...
    > On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>If the user were to close the file, how long does the instance of
    >>XMLParser exist for? Is it disposed of automatically as long as no
    >>references to it exist or do I have to actually dispose of it manually?

    >
    > Presuming any references to the old one are null or are replaced by
    > references to a different object it will hang around until you run out
    > of RAM. There will be a GC pass and all live objects will be scavenged
    > leaving any dead ones behind and their space reclaimed. See
    > http://mindprod.com/jgloss/garbagecollection.html
    >
    > It is considered more hip to create a new object even when the old one
    > will do. Why? Presumably because in recycling there might be some
    > slip in initialisation. Perhaps it is just an unconscious
    > accommodation to consumer culture.


    Also because in many cases the "optimization" of keeping a cache of reusable
    objects around performs worse than letting the GC do its job.

    And also because a reusable object is ipso facto mutable, making it more
    dificult and error-prone to use.
    Mike Schilling, Mar 25, 2006
    #6
  7. "Stephen Marjoribanks" <> wrote in message
    news:e03op9$3vn$...
    > The title may be a little off topic actually but anyway... I have an
    > application which every time an XML file is opened creates an instance of
    > a class called XMLParser. This does not extend any other classes and it's
    > function is to validate the XML file and retrieve all the required data
    > from it and store it in arrays/hashmaps.
    >
    > If the user were to close the file, how long does the instance of
    > XMLParser exist for? Is it disposed of automatically as long as no
    > references to it exist or do I have to actually dispose of it manually?
    >
    > The reason I ask is that should the user open a second XML file after
    > closing the first, another instance of XMLParser is created, with the new
    > arrays holding the new data. When the other classes within the aplpication
    > need to access the data would they not get confused between the first
    > instance of XMLParser and the second? Bearing in mind they will be
    > identical apart from the data stored within the arrays etc?
    >
    > So basically, how do I ensure that the first instance of XMLParser no
    > longer exists or is no longer accessible when a second instance is
    > created?


    No offense meant, Stephen, but you need to find a good introductory Java
    book and read the sections on classes, objects, and garbage collection.
    These are the among the most basic concepts of the language, and until you
    master them, you'll have a difficult time programming in it.
    Mike Schilling, Mar 25, 2006
    #7
  8. Stephen Marjoribanks

    Roedy Green Guest

    On Sat, 25 Mar 2006 17:25:49 +0000, Stephen Marjoribanks
    <> wrote, quoted or indirectly quoted
    someone who said :

    >XMLParser parser = new XMLParser();
    >
    >executed. If the instance of XMLParser 'parser' is still hanging around
    >(even if it is not in use anymore) and a new instance also called
    >'parser' is created (by executing the line of code above again) how will
    >it know which 'parser' to reference?


    If you write

    XMLParser parser = new XMLParser();

    ....
    parser = null;

    theXMLParser object is a candidate for garbage collection.

    If you write:

    parser = new XMLParser();

    You have a second XMLParser object pointed to. Nobody presumably is
    pointing to the old one any more. So the old one is subject to garbage
    collection.

    If somebody else is pointing to it, it won't be GCed.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 25, 2006
    #8
  9. Stephen Marjoribanks

    Stefan Ram Guest

    "Mike Schilling" <> writes:
    >No offense meant, Stephen, but you need to find a good
    >introductory Java book and read the sections on classes,
    >objects, and garbage collection.


    Or: just read about objects and classes and forget
    about garbage collection.

    Eventually, one will need to learn something about garbage
    collection indeed, but it is not urgent.
    Stefan Ram, Mar 25, 2006
    #9
  10. Stephen Marjoribanks

    Roedy Green Guest

    On Sat, 25 Mar 2006 18:17:49 GMT, "Mike Schilling"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Also because in many cases the "optimization" of keeping a cache of reusable
    >objects around performs worse than letting the GC do its job.


    That is if you keep your own pool of recycled objects. That is
    different from simply reusing the object you already have. I think in
    all cases reusing should be faster. The only case I can see where the
    opposite would be true is if you held on to a an object you no longer
    needed on hopes of recycling later. Often you would be better off to
    let it go, uses its ram for ether purposes, and create a new one when
    you needed it.

    It is a bit like the decision you make to rent or own a tent.

    Much of the time though you are creating an object identical to the
    one you are freeing. The main reason then is to recycle you need a
    different mechanism than the constructor for initialisation, which
    might not work exactly the same way. Ideally there would be a way to
    exactly reproduce the constructor logic on a recycle.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 25, 2006
    #10
  11. "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > "Mike Schilling" <> writes:
    >>No offense meant, Stephen, but you need to find a good
    >>introductory Java book and read the sections on classes,
    >>objects, and garbage collection.

    >
    > Or: just read about objects and classes and forget
    > about garbage collection.
    >
    > Eventually, one will need to learn something about garbage
    > collection indeed, but it is not urgent.


    The details of its operation can probably wait, but the fact that it exists
    is important, especially for someone coming from C++ who's wondering about
    object deletion and destructors.
    Mike Schilling, Mar 25, 2006
    #11
  12. Stephen Marjoribanks

    Alex Hunsley Guest

    Stephen Marjoribanks wrote:
    > The title may be a little off topic actually but anyway... I have an
    > application which every time an XML file is opened creates an instance
    > of a class called XMLParser. This does not extend any other classes and
    > it's function is to validate the XML file and retrieve all the required
    > data from it and store it in arrays/hashmaps.


    Others have replied with useful info, and to that I would just like to add:
    There is a certain method you can call - System.gc() - to indicate that
    you would like the JVM to consider doing a garbage collection. It's not
    actually guaranteed that the JVM will a garbage collection when you call
    this method - but regard it as a way of providing a request that garbage
    collection should happen.
    Alex Hunsley, Mar 26, 2006
    #12
  13. Patricia Shanahan wrote:

    > This may be clearer with a shift in mental model of what the line of
    > code says.
    >
    > It does not mean "Create an XMLParser and name it 'parser'". It means
    > "Declare a reference variable, scope from here to the end of the current
    > block, named 'parser' and initialized with a pointer to a new XMLParser".
    >
    > In Java, objects don't have names, so it does not make sense to think of
    > "the instance of XMLParser 'parser'". A reference variable does have a
    > name and is either null or a pointer to some object, so while 'parser'
    > remains in scope, it does make sense to think of "the XMLParser that
    > variable 'parser' currently references".
    >
    > Patricia



    Ah yes, ok thank you, I think this was a missing part in my
    understanding of Java!

    Steve
    Stephen Marjoribanks, Mar 26, 2006
    #13
  14. Mike Schilling wrote:

    >
    > No offense meant, Stephen, but you need to find a good introductory Java
    > book and read the sections on classes, objects, and garbage collection.
    > These are the among the most basic concepts of the language, and until you
    > master them, you'll have a difficult time programming in it.
    >
    >



    No offense taken! I did read a number of Java books and I'm sure I did
    read about this but my mind went blank yesterday and I got confused! I
    am only a newbie to Java ;-)

    Steve
    Stephen Marjoribanks, Mar 26, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    671
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,363
    Anushi
    Jul 29, 2004
  3. Mariusz

    Re: disposing elements in asp pages

    Mariusz, Oct 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    353
    Mariusz
    Oct 22, 2004
  4. Rob

    Disposing

    Rob, Jan 11, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    414
    Karl Seguin [MVP]
    Jan 11, 2007
  5. jon wayne
    Replies:
    9
    Views:
    695
    Jim Langston
    Sep 22, 2005
Loading...

Share This Page