Is it possible to buffer objects during Java serialization?

I have a very large object that I want to serialize. During the serialization process, it takes about 130 MB of heap in the form of weblogic.utils.io.UnsyncByteArrayOutputStream. I use BufferedOutputStream to speed up writing data to disk, which reduces the time this object is stored in memory.

Is it possible to use a buffer to reduce the size of an object in memory? It would be nice if there was a way to serialize it by x bytes at a time and write these bytes to disk.

Sample code follows if used. There are not many, although I do not think so. If this is the case, then there must be a complete copy of the object to be serialized (and therefore not an idea of ​​the serialization buffer), I suppose I'm stuck.

    ObjectOutputStream tmpSerFileObjectStream = null;
    OutputStream tmpSerFileStream = null;
    BufferedOutputStream bufferedStream = null;
    try {

        tmpSerFileStream = new FileOutputStream(tmpSerFile);
        bufferedStream = new BufferedOutputStream(tmpSerFileStream);

        tmpSerFileObjectStream = new ObjectOutputStream(bufferedStream);
        tmpSerFileObjectStream.writeObject(siteGroup);
        tmpSerFileObjectStream.flush();

    } catch (InvalidClassException invalidClassEx) {
        throw new SiteGroupRepositoryException(
                "Problem encountered with class being serialised", invalidClassEx);
    } catch (NotSerializableException notSerializableEx) {
        throw new SiteGroupRepositoryException(
                "Object to be serialized does not implement " + Serializable.class,
                notSerializableEx);
    } catch (IOException ioEx) {
        throw new SiteGroupRepositoryException(
                "Problem encountered while writing ser file", ioEx);
    } catch (Exception ex) {
        throw new SiteGroupRepositoryException(
                "Unexpected exception encountered while writing ser file", ex);
    } finally {
        if (tmpSerFileObjectStream != null) {
            try {
                tmpSerFileObjectStream.close();
                if(null!=tmpSerFileStream)tmpSerFileStream.close();
                if(null!=bufferedStream)bufferedStream.close();
            } catch (IOException ioEx) {
                logger.warn("Exception caught on trying to close ser file stream", ioEx);
            }
        }
    }
+3
5

"siteGroup", ? , , - 130 , ginormous list/array/map/whatever - .

, , bagillion, , , ( () ), .

, , , , , , , , : - .

, clone(), . .

0

. . . ,

  • tomcat.
  • jvms ( -)

Java ( ) .

- , :

  • , JVM.
  • -, "", (, ).
  • , .

, java.io.Serialization. java.io.Externalization . - json xml-.

:

:

.

+2

unsync?

, . , . : .

0

, , , , , , .

, . http://objectmix.com/weblogic/523772-outofmemoryerror-adapter.html

weblogic? unit test? , JVM , .

0

I don't know about weblogic (that is, JRockit, I suppose) serialization in particular: honestly, I see no reason to use ByteArrayOutputStreams ...

You might want to implement java.io.Externalizableit if you need more control over how your object is serialized, or switch to a completely different serialization system (for example: Terracotta ) if you do not want to write read / write methods yourself (if you have many large classes).

0
source

Source: https://habr.com/ru/post/1752598/


All Articles