Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Performance: When clearing a object is it better to set to null or create a new object.

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?

like image 840
Brad Germain Avatar asked Jul 28 '11 15:07

Brad Germain


People also ask

What is the advantage of reusing existing objects?

Advantages of Reuseconserves resources. reduces the waste stream. causes less pollution than recycling or making new products from virgin materials. makes needed items available to those who can't afford to buy them new.

What happens when you set an object to null in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.

Does null extend object Java?

No, null is not an object. It is a literal that means that a variable does not reference any object. Save this answer.

Do null objects require garbage collection?

Not necessarily. An object becomes eligible for garbage collection when there are no live threads anymore that hold a reference to the object. Explicit nulling is simply the practice of setting reference objects to null when you are finished with them.


1 Answers

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.

like image 175
Jon Skeet Avatar answered Sep 30 '22 05:09

Jon Skeet