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

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I got it figured out. the raw html just works… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer Look at the ThreadPool, specifically the QueueUserWorkItem method. It's quite… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer You cannot do this. By definition, generics are a compile-time… May 16, 2026 at 3:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For my understanding purpose i have implemented Chain-Of-Responsibility pattern. //Abstract Base Type public abstract
I have been tasked to develop an interactive website using java & mysql: using
In general, if you have a class that inherits from a Thread class, and
I have implemented a templated buffer class like this: template <class T, class Impl>
For general-purpose sorting, the answer appears to be no, as quick sort, merge sort
We need to implement some general-purpose object structure, much like an object in dynamic
I am trying to implement a scenario using JSF. I have a commandExButton and
I have implemented signals for mousePressEvent() in a QGraphicsScene subclass, but I can't figure
Let's say I have I have an online store with a shopping cart feature
My question is one I have pondered around when working on a demanding network

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.