Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

re-using objects

Status
Not open for further replies.

awolff02

Programmer
Nov 24, 2008
6
0
0
US
Hello,

If in my code I do this:

Code:
File myFile = new File("myfile1.xml");
DocumentBuilderFactory dbfi = DocumentBuilderFactory.newInstance();
DocumentBuilder dbi = dbfi.newDocumentBuilder();
docxml = dbi.parse(File1);

and later on within the same block I do this:

Code:
myFile = new File("myfile2.xml");
DocumentBuilderFactory dbfi = DocumentBuilderFactory.newInstance();
DocumentBuilder dbi = dbfi.newDocumentBuilder();
docxml = dbi.parse(File2);

Is this re-using the objects (myFile and docxml) or am I eating up more memory? I guess the bottom line is I am trying to re-use the objects myFile and docxml so as to reduce the memory requirements of my program.

Thanks for your input.
 
As a general comment, with the RAM and CPU that computers have nowadays, maybe it's not worth considering this.

Appart from that, I don't think you can reuse the File object, as the file name is set on the constructor.

What I'd do

Code:
File myFile1 = new File("myfile1.xml");
DocumentBuilderFactory dbfi = DocumentBuilderFactory.newInstance();
DocumentBuilder dbi = dbfi.newDocumentBuilder();
docxml = dbi.parse(myFile1);

...

File myFile2 = new File("myfile2.xml");
dbfi = DocumentBuilderFactory.newInstance();
dbi = dbfi.newDocumentBuilder();
docxml = dbi.parse(myFile2);

Cheers,
Dian
 
You can read more articles on garbage collection on running task that may last for several days or out of memory error occurs.
 
Each "new File()" reserves memory, so when doing
File myFile = new File("myfile1.xml");
you're reserving some memory space for your object, and when doing
myFile = new File("myfile2.xml");
you're reserving another space of memory for the new object.

So this means you're re-using the object/variable name, but not the object itself, neither it's memory space.
 
But I don't think you can reuse a File object, since the file name is assigned in the constructor ...

Cheers,
Dian
 
When you do:
Code:
 myFile = new File("file1.xml")

you create a file object and store it's handle in myFile.

If you reuse the name myFile later on with
Code:
 myFile = new File("file2.xml")

You are calling the constructor again (new File(...)) and creating a new object and assigning that objects handle in your local variable myFile. Since the first object now has no handle to it the garbage collector will remove it.

You can safely reuse the myFile handle as many times as you like.

In general with java, any object that has no handles to it will be removed by the garbage collector.
 
a) @pointertowhat: Right. A new object is created but the gc will not remove the old object - it may remove the old object.

b) The original question contains code which will not compile, since in the same block you may not redeclare dbfi and dbi, not even in the same way.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top