Java 快速深度克隆对象 [Faster Deep Copies of Java Objects]
最近深刻糾結于復雜對象的clone, 實在苦惱于寫多個對象的clone函數,于是收集網上的deep clone方法,真是適合我這種懶人~~
The java.lang.Object root superclass defines a clone() method that will, assuming the subclass implements thejava.lang.Cloneable interface, return a copy of the object. While Java classes are free to override this method to do more complex kinds of cloning, the default behavior ofclone() is to return a shallow copy of the object. This means that the values of all of the origical object’s fields are copied to the fields of the new object.
A property of shallow copies is that fields that refer to other objects will point tothe same objects in both the original and the clone. For fields that contain primitive or immutable values (int,String, float, etc…), there is little chance of this causing problems. For mutable objects, however, cloning can lead to unexpected results. Figure 1 shows an example.
import java.util.Vector;public class Example1 {public static void main(String[] args) {// Make a VectorVector original = new Vector();// Make a StringBuffer and add it to the VectorStringBuffer text = new StringBuffer("The quick brown fox");original.addElement(text);// Clone the vector and print out the contentsVector clone = (Vector) original.clone();System.out.println("A. After cloning");printVectorContents(original, "original");printVectorContents(clone, "clone");System.out.println("--------------------------------------------------------");System.out.println();// Add another object (an Integer) to the clone and// print out the contentsclone.addElement(new Integer(5));System.out.println("B. After adding an Integer to the clone");printVectorContents(original, "original");printVectorContents(clone, "clone");System.out.println("--------------------------------------------------------");System.out.println();// Change the StringBuffer contentstext.append(" jumps over the lazy dog.");System.out.println("C. After modifying one of original's elements");printVectorContents(original, "original");printVectorContents(clone, "clone");System.out.println("--------------------------------------------------------");System.out.println();}public static void printVectorContents(Vector v, String name) {System.out.println(" Contents of \"" + name + "\":");// For each element in the vector, print out the index, the// class of the element, and the element itselffor (int i = 0; i < v.size(); i++) {Object element = v.elementAt(i);System.out.println(" " + i + " (" +element.getClass().getName() + "): " +element);}System.out.println();}}
Figure 1. Modifying Vector contents after cloning
In this example we create a Vector and add a StringBuffer to it. Note that StringBuffer (unlike, for example, String is mutable — it’s contents can be changed after creation. Figure 2 shows the output of the example in Figure 1.
Figure 2. Output from the example code in Figure 1
In the first block of output (”A”), we see that the clone operation was successful: The original vector and the clone have the same size (1), content types, and values. The second block of output (”B”) shows that the original vector and its clone are distinct objects. If we add another element to the clone, it only appears in the clone, and not in the original. The third block of output (”C”) is, however, a little trickier. Modifying theStringBuffer that was added to the original vector has changed the value of the first element ofboth the original vector and its clone. The explanation for this lies in the fact thatclone made a shallow copy of the vector, so both vectors now point to the exact sameStringBuffer instance.
This is, of course, sometimes exactly the behavior that you need. In other cases, however, it can lead to frustrating and inexplicable errors, as the state of an object seems to change “behind your back”.
The solution to this problem is to make a deep copy of the object. A deep copy makes a distinct copy of each of the object’s fields, recursing through the entire graph of other objects referenced by the object being copied. The Java API provides no deep-copy equivalent to Object.clone(). One solution is to simply implement your own custom method (e.g.,deepCopy()) that returns a deep copy of an instance of one of your classes. This may be the best solution if you need a complex mixture of deep and shallow copies for different fields, but has a few significant drawbacks:
A common solution to the deep copy problem is to use Java Object Serialization (JOS). The idea is simple: Write the object to an array using JOS’sObjectOutputStream and then use ObjectInputStream to reconsistute a copy of the object. The result will be a completely distinct object, with completely distinct referenced objects. JOS takes care of all of the details: superclass fields, following object graphs, and handling repeated references to the same object within the graph. Figure 3 shows a first draft of a utility class that uses JOS for making deep copies.
import java.io.IOException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream;/*** Utility for making deep copies (vs. clone()'s shallow copies) of* objects. Objects are first serialized and then deserialized. Error* checking is fairly minimal in this implementation. If an object is* encountered that cannot be serialized (or that references an object* that cannot be serialized) an error is printed to System.err and* null is returned. Depending on your specific application, it might* make more sense to have copy(...) re-throw the exception.** A later version of this class includes some minor optimizations.*/ public class UnoptimizedDeepCopy {/*** Returns a copy of the object, or null if the object cannot* be serialized.*/public static Object copy(Object orig) {Object obj = null;try {// Write the object out to a byte arrayByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream out = new ObjectOutputStream(bos);out.writeObject(orig);out.flush();out.close();// Make an input stream from the byte array and read// a copy of the object back in.ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));obj = in.readObject();}catch(IOException e) {e.printStackTrace();}catch(ClassNotFoundException cnfe) {cnfe.printStackTrace();}return obj;}}Figure 3. Using Java Object Serialization to make a deep copy
Unfortunately, this approach has some problems, too:
The first two of these problems cannot be addressed in a general way. We can, however, use alternative implementations ofByteArrayOutputStream and ByteArrayInputStream that makes three simple optimizations:
An optimized implementation of ByteArrayOutputStream is shown in Figure 4. import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayInputStream;/*** ByteArrayOutputStream implementation that doesn't synchronize methods* and doesn't copy the data on toByteArray().*/ public class FastByteArrayOutputStream extends OutputStream {/*** Buffer and size*/protected byte[] buf = null;protected int size = 0;/*** Constructs a stream with buffer capacity size 5K*/public FastByteArrayOutputStream() {this(5 * 1024);}/*** Constructs a stream with the given initial size*/public FastByteArrayOutputStream(int initSize) {this.size = 0;this.buf = new byte[initSize];}/*** Ensures that we have a large enough buffer for the given size.*/private void verifyBufferSize(int sz) {if (sz > buf.length) {byte[] old = buf;buf = new byte[Math.max(sz, 2 * buf.length )];System.arraycopy(old, 0, buf, 0, old.length);old = null;}}public int getSize() {return size;}/*** Returns the byte array containing the written data. Note that this* array will almost always be larger than the amount of data actually* written.*/public byte[] getByteArray() {return buf;}public final void write(byte b[]) {verifyBufferSize(size + b.length);System.arraycopy(b, 0, buf, size, b.length);size += b.length;}public final void write(byte b[], int off, int len) {verifyBufferSize(size + len);System.arraycopy(b, off, buf, size, len);size += len;}public final void write(int b) {verifyBufferSize(size + 1);buf[size++] = (byte) b;}public void reset() {size = 0;}/*** Returns a ByteArrayInputStream for reading back the written data*/public InputStream getInputStream() {return new FastByteArrayInputStream(buf, size);}}
Figure 4. Optimized version of ByteArrayOutputStream The getInputStream() method returns an instance of an optimized version of ByteArrayInputStream that has unsychronized methods. The implementation of FastByteArrayInputStream is shown in Figure 5.
Figure 5. Optimized version of ByteArrayInputStream.
Figure 6 shows a version of a deep copy utility that uses these classes: import java.io.IOException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream;/*** Utility for making deep copies (vs. clone()'s shallow copies) of* objects. Objects are first serialized and then deserialized. Error* checking is fairly minimal in this implementation. If an object is* encountered that cannot be serialized (or that references an object* that cannot be serialized) an error is printed to System.err and* null is returned. Depending on your specific application, it might* make more sense to have copy(...) re-throw the exception.*/ public class DeepCopy {/*** Returns a copy of the object, or null if the object cannot* be serialized.*/public static Object copy(Object orig) {Object obj = null;try {// Write the object out to a byte arrayFastByteArrayOutputStream fbos =new FastByteArrayOutputStream();ObjectOutputStream out = new ObjectOutputStream(fbos);out.writeObject(orig);out.flush();out.close();// Retrieve an input stream from the byte array and read// a copy of the object back in.ObjectInputStream in =new ObjectInputStream(fbos.getInputStream());obj = in.readObject();}catch(IOException e) {e.printStackTrace();}catch(ClassNotFoundException cnfe) {cnfe.printStackTrace();}return obj;}}
Figure 6. Deep-copy implementation using optimized byte array streams
The extent of the speed boost will depend on a number of factors in your specific application (more on this later), but the simple class shown in Figure 7 tests the optimized and unoptimized versions of the deep copy utility by repeatedly copying a large object.
import java.util.Hashtable; import java.util.Vector; import java.util.Date;public class SpeedTest {public static void main(String[] args) {// Make a reasonable large test object. Note that this doesn't// do anything useful -- it is simply intended to be large, have// several levels of references, and be somewhat random. We start// with a hashtable and add vectors to it, where each element in// the vector is a Date object (initialized to the current time),// a semi-random string, and a (circular) reference back to the// object itself. In this case the resulting object produces// a serialized representation that is approximate 700K.Hashtable obj = new Hashtable();for (int i = 0; i < 100; i++) {Vector v = new Vector();for (int j = 0; j < 100; j++) {v.addElement(new Object[] {new Date(),"A random number: " + Math.random(),obj});}obj.put(new Integer(i), v);} int iterations = 10;// Make copies of the object using the unoptimized version// of the deep copy utility.long unoptimizedTime = 0L;for (int i = 0; i < iterations; i++) {long start = System.currentTimeMillis();Object copy = UnoptimizedDeepCopy.copy(obj);unoptimizedTime += (System.currentTimeMillis() - start);// Avoid having GC run while we are timing...copy = null;System.gc();}// Repeat with the optimized versionlong optimizedTime = 0L;for (int i = 0; i < iterations; i++) {long start = System.currentTimeMillis();Object copy = DeepCopy.copy(obj);optimizedTime += (System.currentTimeMillis() - start);// Avoid having GC run while we are timing...copy = null;System.gc();}System.out.println("Unoptimized time: " + unoptimizedTime);System.out.println(" Optimized time: " + optimizedTime);}}Figure 7. Testing the two deep copy implementations.
A few notes about this test:
- The object that we are copying is large. While somewhat random, it will generally have a serialized size of around 700 Kbytes.
- The most significant speed boost comes from avoid extra copying of data in FastByteArrayOutputStream. This has several implications:
- Using the unsynchronized FastByteArrayInputStream speeds things up a little, but the standard java.io.ByteArrayInputStream is nearly as fast.
- Performance is mildly sensitive to the initial buffer size in FastByteArrayOutputStream, but is much more sensitive to the rate at which the buffer grows. If the objects you are copying tend to be of similar size, copying will be much faster if you initialize the buffer size and tweak the rate of growth.
- Measuring speed using elapsed time between two calls to System.currentTimeMillis() is problematic, but for single-threaded applications and testing relatively slow operations it is sufficient. A number of commercial tools (such as JProfiler) will give more accurate per-method timing data.
- Testing code in a loop is also problematic, since the first few iterations will be slower until HotSpot decides to compile the code. Testing larger numbers of iterations aleviates this problems.
- Garbage collection further complicates matters, particularly in cases where lots of memory is allocated. In this example, we manually invoke the garbage collector after each copy to try to keep it from running while a copy is in progress.
These caveats aside, the performance difference is sigificant. For example, the code as shown in Figure 7 (on a 500Mhz G3 Macintosh iBook running OSX 10.3 and Java 1.4.1) reveals that the unoptimized version requires about 1.8 seconds per copy, while the optimized version only requires about 1.3 seconds. Whether or not this difference is signficant will, of course, depend on the frequency with which your application does deep copies and the size of the objects being copied.
For very large objects, an extension to this approach can reduce the peak memory footprint by serializing and deserializing in parallel threads. See “Low-Memory Deep Copy Technique for Java Objects” for more information.
摘自http://javatechniques.com/blog/faster-deep-copies-of-java-objects/
總結
以上是生活随笔為你收集整理的Java 快速深度克隆对象 [Faster Deep Copies of Java Objects]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Varnish——CDN推送平台管理(w
- 下一篇: JavaWeb项目实践