Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Disposing of an object?

Reply
Thread Tools

Disposing of an object?

 
 
Stephen Marjoribanks
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      03-25-2006
On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Naveed
Guest
Posts: n/a
 
      03-25-2006
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.

 
Reply With Quote
 
Stephen Marjoribanks
Guest
Posts: n/a
 
      03-25-2006
Roedy Green wrote:
> On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-25-2006

"Roedy Green" <(E-Mail Removed) > wrote in
message news(E-Mail Removed)...
> On Sat, 25 Mar 2006 15:49:11 +0000, Stephen Marjoribanks
> <(E-Mail Removed)> 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.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-25-2006
"Stephen Marjoribanks" <(E-Mail Removed)> wrote in message
news:e03op9$3vn$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-25-2006
On Sat, 25 Mar 2006 17:25:49 +0000, Stephen Marjoribanks
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-25-2006
"Mike Schilling" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-25-2006
On Sat, 25 Mar 2006 18:17:49 GMT, "Mike Schilling"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disposing of old computers/printers, etc ANovice Computer Support 5 04-19-2007 01:38 PM
Disposing Rob ASP .Net 4 01-11-2007 01:47 PM
Re: disposing elements in asp pages Mariusz ASP .Net 5 10-22-2004 10:41 AM



Advertisments