• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Cloning Of an Java Object

 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

If we clone an Object , does the cloned object will be responsible to occupy memory on to the heap ??
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi,
I don't follow. A cloned object uses separate memory. The JVM handles memory.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i mean to ask is that excessive cloning also results in wastage of the memory know ??

and what do you mean separate memory ??(where actually)
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ravi

There are 2 two types of cloning based on your requirments.

1) Deep Copy ...... where there is a copy of the whole object and its members are created which will be separate memory location on heap.

2) Shallow Copy .... I guess will copy only reference ..... which will point to same memory location !!!



I am I right ............here
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RaviNada Kiran wrote:Hi all ,

If we clone an Object , does the cloned object will be responsible to occupy memory on to the heap ??



those objects that are running or called in method are on heap...
so if the clonned object is being used it will remain on heap till it finishes its operations that time
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both Deep and shallow clone Occupy memory

Deep would occupy more space since it copies objects recursively whereas shallow will not.

As far as wastage of memory is concerned,
- you dont need to bother about it, the jvm manages it all
- you can invoke the System.gc(); command to suggest a garbage collection
- Excessive cloning would result in excessive use of memory.

What i fail to understand is your defination of memory wastage.
If an application requires 10 Objects (or clones) to execute, so be it!!! The optimization you can use is to ensure minimal data structures.

If you are a C++ programmer migrating to java, Java does not expect the programmer to free the memory or to create memory blocks
so there is no question of them being wasted or saved. Optimizations lie in program logic itself.
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir

Please try to help me out here ....
If the shallow copy and the original object both are different memory locations ....
Does the JVM put extra effort to synchronize both the copies ......


Thanks

Sandeep Kumar Jakkaraju

" I dont just COPY the JAVA code..... but try to understand it !!"
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandeep Kumar Jakkaraju wrote:If the shallow copy and the original object both are different memory locations ....
Does the JVM put extra effort to synchronize both the copies ......


Why would the copies be synchronized? They are (usually*) two different objects that know nothing about each other. Their reference fields point to the same objects, so if they have a StringBuilder field, changing it in one of the copies will lead to changes in the other copy. Not because of synchronization, but because they use the same object.

As for the original question: when you (shallowly) clone an object, you are creating a new object. It would take the same amount of memory as a call to "new" would, with all reference variables simply copied. Just like creating many new objects, cloning a lot will lead to a memory increase if the objects are not garbage collected. For instance:

In the end, 20000 extra Date objects are created.



* Object.clone() may return "this", as specified in the API, although it is highly unusual. Of course, if this is the case, no extra objects are created so memory usage will remain the same.
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sir

I mean the same thing ,.......


If you override the clone() method of the Class you are implmenting .... to create a copy ... it is usually a deep copy and there are 2 objects in the JVM occupying the double the memory on the heap .....

I dont expect a layman like me to override the clone() method in each and every class i write so ..... i will always get a shallow copy hence there is only 1 object in the heap with two references pointing to it !!!



I am right again .... one more step towards becoming a bartender !!!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandeep Kumar Jakkaraju wrote:Dear Sir

I mean the same thing ,.......


If you override the clone() method of the Class you are implmenting .... to create a copy ... it is usually a deep copy and there are 2 objects in the JVM occupying the double the memory on the heap .....

I dont expect a layman like me to override the clone() method in each and every class i write so ..... i will always get a shallow copy hence there is only 1 object in the heap with two references pointing to it !!!


If you mean one shared field object, then you are right. But clone() does create a new object of the class it is called on (usually, see the API again).

Consider the following example:

ShallowClone.clone() will create one additional object; both ShallowClone object share the same Date objects. So there will be three objects: two ShallowClone objects and one Date object
DeepClone.clone() will create two additional objects; each DeepClone object has its own Date object. So there will be four objects: two DeepClone objects and two Date objects.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sandeep, what is the problem that you are facing in cloning ?

To use Shallow or Deep copy is based on the programming approach you use. I am lost over here as per what are your requirements.

(please dont call me "sir" too)

btw, Shallow copy Does create 1 Seperate Object !!!
it does not recursively create Multiple Objects

so, if the original obj is changed, the shallow copy is not affected.
but if the original copy has a reference and the reference value changes, the shallow copy is affected.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this clears your doubt

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic