-2

How to mesure size of my object, it means size of memory taked (occuped) by my object serialized please ?

FileOutputStream fos = new FileOutputStream("personne.serial");

    ObjectOutputStream oos = new ObjectOutputStream(fos);
    try {
        oos.writeObject(p);
        oos.flush();
    } finally {
        try {
            oos.close();
        } finally {
            fos.close();
        }
    }
Mehdi
  • 107
  • 9
  • i don't know what's not clear ! i have an object serialized, i want to calculate memory occuped by this object in memory ! what is not clear ? – Mehdi Mar 22 '12 at 09:31
  • possible duplicate of [Size of object serialized](http://stackoverflow.com/questions/9791245/size-of-object-serialized) – skaffman Mar 22 '12 at 09:35
  • @skaffman : In last question, a final reponse is about serialization in table but her in FIle ! – Mehdi Mar 22 '12 at 09:39
  • The question is pretty unclear Size of what... physcial lenght. File on disk .. memory footprint ... what? – krystan honour Mar 22 '12 at 10:04
  • 1
    Now its clear, you didn't say "size in memory" – krystan honour Mar 22 '12 at 10:06
  • 1
    @Mehdi: You've asked a series of very similar questions without explaining how they differ from the previous ones, and without explaining why you haven't edited them for clarity or accepted anyone's answers to them. If you keep doing this, noone's going to put any effort in. – skaffman Mar 22 '12 at 10:13
  • @skaffman: it's true, sorry! In my last topic, i have asked how i can calculate size of memory occuped by an abject serialized in file. Someone answer me that you can serialized under table like `ByteArrayOutputStream` and calculate it , it's very simple oky but when i use it i have an exception `java.lang.StackOverflowError` : `public void serializ(CRDT m ) throws IOException { System.out.println("ok"); ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(byteOutput); stream.writeObject(m);}` – Mehdi Mar 22 '12 at 10:32

1 Answers1

0

As far as I am aware . The amount of memory an object occupies is implementation dependent it might be different on different JVMs and also depend the platform the jvm is running in. I do not believe that using stand apis its possible to calculate this accurately.

krystan honour
  • 6,523
  • 3
  • 36
  • 63
  • But when i use ByteArrayOutputStream to serialize an object i have an exception `java.lang.StackOverflowError`, there are a solution to extend length of the ByteArrayOutputStream ? `ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();` – Mehdi Mar 22 '12 at 10:37
  • Going to need to see more code here, need to see the object you are serialising and typical content. – krystan honour Mar 22 '12 at 10:44
  • An object seriliazed is very large, it's a distributed algorithm with several object! – Mehdi Mar 22 '12 at 10:48
  • I serialize with this method : `public void serializ(Object m ) throws IOException { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(byteOutput); stream.writeObject(m); sumMemory += byteOutput.toByteArray().length; }` – Mehdi Mar 22 '12 at 10:50
  • good explanation of possible issue here http://stackoverflow.com/questions/438875/stackoverflowerror-when-serializing-an-object-in-java – krystan honour Mar 22 '12 at 11:43