aspose file tools*
The moose likes Performance and the fly likes Creating an object vs copying it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Creating an object vs copying it" Watch "Creating an object vs copying it" New topic
Author

Creating an object vs copying it

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
I have read somewhere that when the cost of creating a new object is large and creation is resource intensive, we clone the object.

Is copying an object(using cloning) instead of creating a new one is better in terms of performance?

If yes, How?

Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Normally, copying an object is much faster than creating a new one.
The following link contains a good explanation: http://en.wikibooks.org/wiki/Java_Programming/Creating_Objects


Blackbelt on BlackBeltFactory.com.
anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
Thanks for sharing the link.

But it doesn't explain why creating an object using "new" is time consuming than doing the same by copying it.

Also, is the object created in heap,as in the case with "new", when we copy an object using clone()?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Whether copying is significantly faster than new depends entirely on how complex the object is. Furthermore, copying can get you in a LOT of trouble if you don't think things through.

For example suppose you have an object that creates a database connection - an expensive operation. If you clone that object, both the old and the new will have a reference to the SAME database connection - just imagine how that will work out! It might look great until two separate Threads try to use the connection "at the same time".

Personally, I would never touch cloning as a performance improvement option. Too many chances for unexpected interactions. Object creation by new has been improved in speed as Java has evolved.

Bill


Java Resources at www.wbrogden.com
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
The jvm does a lot of magic under the hood. For example a new can be highly optimized so a 'true' OS memory allocation never occurs. Here is a great well written document that talks of garbage collection, but also touches on memory management in general.

http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf

Here is an excerpt.

"Fast Allocation
As you will see from the garbage collector descriptions below, in many cases there are large contiguous blocks of memory available from which to allocate objects. Allocations from such blocks are efficient, using a simple bump-the-pointer technique. That is, the end of the previously allocated object is always kept track of. When a new allocation request needs to be satisfied, all that needs to be done is to check whether the object will fit in the remaining part of the generation and, if so, to update the pointer and initialize the object."

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an object vs copying it
 
Similar Threads
Pass By Value
addAll(); in the Collection
copy files from one folder to another in java
resize two arrays ... guide me ...
Prototype Pattern?