When using an object repeatedly, is it better to clear the data by setting it to null, or instantiate a new object?
Object a = new Object();
for(...){
...
a = null;
**OR**
a = new Object();
}
Here is the example I'm referring to:
Customer a = new Customer();
Collection<Customer> data = new ArrayList<Customer>();
while (rs != null && rs.next()) {
a = new Customer();
a.setCustId(rs.getLong("CUST_ID"));
a.setPerNo(period);
a.setName(rs.getString("cust_nm"));
if(a!= null)
data.add(a);
}
I'm wondering whether the
a = new Customer();
is the best way to do this or if it should be done differently to save on memory and for optimum performance, because each loop has new customer information to put in. From my understanding, if you create a new customer you're creating a new object 开发者_开发百科and pointing a to that new object. So the old object a was pointing to will get picked up by the garbage collector - same in the case of setting it to null. Is this a correct understanding?
I would prefer:
for (...) {
Object a = new Object();
...
}
Why make the scope any greater than it needs to be?
Likewise, unless I needed a new object, I wouldn't create one for the sake of it.
It's hard to say much more from just the description given though - if you can give a more complete and real-world example, we may be able to make more concrete recommendations.
(Note that there's no indication that you want to use an object repeatedly, so much as using a variable repeatedly. They're very different concepts.)
EDIT: Looking at your specific example, I'd write it like this:
Collection<Customer> data = new ArrayList<Customer>();
while (rs != null && rs.next()) {
Customer a = new Customer();
a.setCustId(rs.getLong("CUST_ID"));
a.setPerNo(period);
a.setName(rs.getString("cust_nm"));
data.add(a);
}
Note that this doesn't create any Customer
objects which are eligible for garbage collection - whereas your original code creates an instance of Customer
before entering the loop, and then ignores the newly created object, instead creating a new one and reassigning a
's value.
Instantiate objects where you need them. Don't where you don't. And don't worry about this level of optimization until a profiler tells you to.
Edit: Given your updated answer, it would be clearer to readers to declare the variable and instantiate your Customer objects inside the loop, as Jon recommended. As to your performance and garbage collection questions, you need to improve your grasp of the basic language concepts.
Explicitly NULLing references is required for special cases wherein you explicitly want to "stop" referring to an object and failure to do so would result in an actual memory leak. As an example, you can have a look at the array backed List
implementation which NULLs array slots to avoid pointing to references/objects which are no longer used.
I think your confusion comes from not understanding that objects are passed by reference. If you're coming from a C background, understand that in Java, Customer a = new Customer();
actually does something like
Customer* a = (Customer*) malloc (sizeof(Customer));
initializeCustomer (a); //the constructor initializes the state of the object.
Since you actually need new memory for each customer you add to your list, you cannot avoid creating a new Customer object for each iteration of the loop.
精彩评论