0

i want to calculate size of object p in this example and size of p serialized:

public class Main {

    static public void main(String[] args) throws IOException {
        Personne p1 = new Personne("name1", "username1", 25);
        SrzDrz sr = new SrzDrz(p1, "file1");

        // Calculate size of(sr) and p1 ???
    }
}

Class Personne is :

public class Personne implements Serializable {

    static private final long serialVersionUID = 6L;
    private String nom;
    private String prenom;
    private Integer age;

    public Personne(String nom, String prenom, Integer age) {
        this.nom = nom;
        this.prenom = prenom;
        this.age = age;
    }

    public String toString() {
        return nom + " " + prenom + " " + age + " years";
    }
}

Class SrzDrz is :

public class SrzDrz {

    SrzDrz(Personne p, String name) throws IOException {

        FileOutputStream fos = new FileOutputStream(name);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        try {
            oos.writeObject(p);
            oos.flush();
            System.out.println(p + " serialized");
        } finally {
            try {
                oos.close();
            } finally {
                fos.close();
            }
        }
    }
}
Adam
  • 35,919
  • 9
  • 100
  • 137
Mehdi
  • 107
  • 9
  • Asking the same thing again won't help, you know... http://stackoverflow.com/questions/9789045/memory-occuped-after-deserialization – skaffman Mar 20 '12 at 16:50
  • No it's not same, here i ask how calculate the memory of simple object ! and at the last question after serialization !!!!!!!!!! – Mehdi Mar 20 '12 at 16:53
  • possible duplicate of [What is the memory consumption of an object in Java?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java) – dty Mar 20 '12 at 16:58

2 Answers2

4

How about this? Just write into a ByteArrayOutputStream and see how big it gets...

ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteOutput);
stream.writeObject(p1);
stream.close();
System.out.println("Bytes = " + byteOutput.toByteArray().length);

Output

Bytes = 200
Adam
  • 35,919
  • 9
  • 100
  • 137
  • if i understand your program, i don't need to serialize in file but in byteOutput ? – Mehdi Mar 20 '12 at 17:06
  • 1
    Yes you can just serialize into byteOutput, unless you have other reasons for writing to disk. – Adam Mar 20 '12 at 17:08
  • Ok thank you for your help :) just last question : if byteOutput does not wrinte to disk, whers it write ? – Mehdi Mar 20 '12 at 17:10
  • 1
    It only exists in memory. It's a wrapper onto an array that conforms to the same interface as FileOutputStream, the only difference is that when ObjectOutStream writes to it, it writes to a byte array instead of disk, it resizes the array as necessary to fit whatever is thrown at it. – Adam Mar 20 '12 at 17:15
0

Try using the File.length() method to get the size of the file you wrote the object to.

If you want to know the size of the object in memory, try this:

http://www.javamex.com/classmexer/

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177