Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's more efficient? to empty an object or create a new one?

how expensive is 'new'? I mean, should I aim at reusing the same object or if the object is 'out of scope' it's the same as emptying it?

example, say a method creates a list:

List<Integer> list = new ArrayList<Integer>(); 

at the end of the method the list is no longer in use - does it mean that there's no memory allocated to it anymore or does it mean that there's a null pointer to it (since it was 'created').

Alternately, I can send a 'list' to the method and empty it at the end of the method with: list.removeAll(list); will that make any difference from memory point of view?

Thanks!

like image 223
adhg Avatar asked Feb 16 '12 15:02

adhg


People also ask

What happens when a new object is created?

When an object is created, memory is allocated to hold the object properties. An object reference pointing to that memory location is also created. To use the object in the future, that object reference has to be stored as a local variable or as an object member variable. Code section 4.30: Object creation.

Which method is called when a new object is created?

Instantiation: The new keyword is a Java operator that creates the object. As discussed below, this is also known as instantiating a class.

Why we use new while creating object?

Answer: The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.


2 Answers

its an array list, so creating a new object means allocating a slab of memory and zeroing it, plus any bookkeeping overhead. Clearing the list means zeroing the memory. This view would lead you to believe that clearing an existing object is faster. But, it's likely that the JVM is optimized to make memory allocations fast, so probably none of this matters. So just write clear, readable code, and don't worry about it. This is java after all, not c.

like image 91
Kevin Avatar answered Oct 26 '22 11:10

Kevin


at the end of the method the list is no longer in use - does it mean that there's no memory allocated to it anymore or does it mean that there's a null pointer to it (since it was 'created').

Means there are no references to it and object is eligible for GC.

Alternately, I can send a 'list' to the method and empty it at the end of the method with: list.removeAll(list); will that make any difference from memory point of view?

It's tradeoff between time/space. Removing elements from list is time consuming, even though you don't need to create new objects.

With the latest JVMs GC collection capabilities, it is ok to create new object WHEN REQUIRED (but avoiding object creation in loop is best). Longer references to an object sometimes make that object NOT eligible for GC and may cause memory leak if not handled properly.

like image 21
kosa Avatar answered Oct 26 '22 11:10

kosa