• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an object vs copying it

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic