Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 November 2013

Java Best Practice – Reuse Objects instead of creating new ones if possible


Reuse Objects instead of creating new ones if possible

Object creation is an expensive operation in Java, with impact on both performance and memory utilization.The cost varies depending on the amount of initialization that needs to be performed when the object is created.
Way to minimize excess object creation are
  1. Use a pool to share resource objects
  2. Recycle Objects
  3. Use Lazy initialization  of object
Use a pool to share resource objects
The resource objects are Threads, JDBC connections, sockets and complex user defined objects. They are expensive to create, and pooling them reduce the overhead of repetitively creating and destroying.On the down side, using a pool means you must implement the code to manage it and pay overhead of synchronization when you get or remove objects from the pool, but the overall performance gains if you get from using pool to  manage expensive resource object outweighs that overhead.
However, be cautious on implementing a resource pool.The following mistakes in pool management are often observed:
  1. a resource object which should be used only serially is given to more than one user at the same time
  2. objects that are returned to the pool are not properly accounted for and are therefore not reused, wasting resources and causing a memory  leak
  3. element or objects references kept in the pool are not reset or cleaned up properly being given to next user.
These mistakes can have severe consequence including data corruption, memory leaks, a race conditions, or even a security problem.My advice in managing is pool is keep your algorithm simple.
Recycle Objects
Recycle object is similar to creating an object pool, but there is no need to manage it because the pool only has one object. This approach is most useful for relatively large container objects (such as Vectors or Hashtable ) that you want to use for holding some temporary data.Reusing of objects instead of creating new ones each time avoid memory allocation and reduce garbage collection.
Similar to using a pool, you must take precautions to clear all the element in any recycled object before you reuse it to avoid memory leak.The collection interface have built-in clear()  method that you can use.If you are building your objects, you should remember to include a reset() or clear() method if necessary.
Use lazy initialization to defer creating the object until you need it
Defer creating an object until its is needed if the initialization of the object is expensive or if the objects is needed only some specific condition.
Example
import java.util.ArrayList;
import java.util.List;


public class JavaTutorialsCorner {
private List tutorials= null;

public List getTutorials(){

if(tutorials == null){
tutorials = new ArrayList<>();
}
return tutorials;
}

}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to configure Maven to use Java 8 In pom.xml, defined this maven.compiler.source properties to configure Maven to use Java 8 to compile the project. Sometimes when you may need to compile a certain project to a different version than what you are currently … Read More
  • Java 8 forEach with List example In this article we are going to see about Java 8 forEach with List example program ForEachList.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; public class ForEachList { pub… Read More
  • Java 8 Stream Filter and Collect Example In this article we are going to see about Java 8 Stream filter() and collect()  example program. StreamFilter.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; import java.… Read More
  • Java 8 Stream Filter with findAny orElse Example In this article we are going to see about Java 8 Stream filter() , findAny() and orElse() example program. Employee.java package com.javatutorialcorner.java8; public class Employee { private Str… Read More
  • Java 8 forEach with Map Example In this article we are going to see about Java 8 forEach example program ForEachMap.java package com.javatutorialcorner.java8; import java.util.HashMap; import java.util.Map; public class ForEachMap { public static vo… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java Best Practice – Reuse Objects instead of creating new ones if possible Rating: 5 Reviewed By: eHowToNow