Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:39:48+00:00 2026-05-16T14:39:48+00:00

We have implemented a general purpose deep copy mechanism using serialization. import java.io.*; public

  • 0

We have implemented a general purpose deep copy mechanism using serialization.

import java.io.*;

public class CopyUtil {

    public static Object clone(Object source) {
        Object retVal = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(source);
            oos.flush();
            oos.close();

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
            retVal = in.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return retVal;
    }
}

There are a relatively large number of object classes, that are evolving all the time, to maintain – This was the reason why we proceeded with a general purpose cloning mechanism. We didn’t relish the idea of maintaining readObject() and writeObject() on 200+ classes.

Unfortunately the serialization mechanism in Java is relatively slow and we are experiencing issues when our system is under peak load.

Are there any suggested approaches on how we can speed things up a bit or (in case we have carried this out incorrectly) alternative methods of cloning objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:39:48+00:00Added an answer on May 16, 2026 at 2:39 pm

    A much faster alternative to serialization is implemented in Hibernate (specifically in the 2nd level cache); I don’t know the details but you can check out the source code.

    You may be aware that the clone() interface is broken, thus is better avoided, unless there is a really really compelling reason to use it. From Effective Java 2nd Edition, Item 11: Override clone judiciously

    Given all of the problems associated with Cloneable, it’s safe to say that
    other interfaces should not extend it, and that classes designed for inheritance
    (Item 17) should not implement it. Because of its many shortcomings, some
    expert programmers simply choose never to override the clone method and never
    to invoke it except, perhaps, to copy arrays. If you design a class for inheritance,
    be aware that if you choose not to provide a well-behaved protected clone
    method, it will be impossible for subclasses to implement Cloneable.

    Update: On shallow/deep cloning

    From the clone() API:

    Creates and returns a copy of this object. The precise meaning of “copy” may depend on the class of the object. […]

    By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal “deep structure” of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

    So in fact, the convention is to do a deep copy.

    Still, the preferred alternative is to define a copy constructor or an independent method instead of overriding clone().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume you have this: // General purpose public interface ISerializer { IDataResult Serialize<T>(T instance);
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond
I have implemented a very basic sign up using email address+name, although I would
What is the general algorithm used by a syntax highlighter? I have implemented a
I am using a counter plugin for jquery (http://wplayground.comuv.com/counter/) and have it implemented successfully
In general, if you have a class that inherits from a Thread class, and
I have implemented 404 handling for the general case in ASP.NET MVC 3, for
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented pagination to my data, but the problem is I only have
I have implemented a table view with multiple threads. Currently when a user taps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.