File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Performance
Bookmark "Creating an object vs copying it" Watch "Creating an object vs copying it" New topic

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:

Blackbelt on
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

Joined: Mar 22, 2000
Posts: 13037
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.


steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
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.

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." - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
I agree. Here's the link:
subject: Creating an object vs copying it
It's not a secret anymore!