2012-03-20 4 views
0

は、私がこの例とシリアライズされたPのサイズにオブジェクトpの大きさを計算したい連載:サイズ

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 ??? 
    } 
} 

クラスPERSONNEです:

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"; 
    } 
} 

クラスSrzDrzは次のとおりです。

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(); 
      } 
     } 
    } 
} 
+0

助けにはなりません再び同じことを尋ねる... ByteArrayOutputStreamに書き込むと、それを取得どのように大きな見、あなたが知っている... http://stackoverflow.com/questions/9789045 /メモリ占有後デシリアライゼーション – skaffman

+0

いいえ、それは同じではない、ここで私はどのように単純なオブジェクトのメモリを計算するか尋ねる!シリアライズ後の最後の質問!!!!!!!!!! – Mehdi

+0

[Javaのオブジェクトのメモリ消費量は?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java)の可能な複製 – dty

答えて

0

File.length()メソッドを使用して、オブジェクトを書き込んだファイルのサイズを取得してみてください。

あなたがメモリ内のオブジェクトのサイズを知りたい場合は、この試してみてください。これについて

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

+0

私はどんなタイプのファイルも使用しません! – Mehdi

+0

オブジェクトを使用せずにオブジェクトのメモリのサイズを計算する方法はありませんか? – Mehdi

+0

@user - FileOutputStreamを使用してファイルを作成します。 –

4

どのように?ただ、

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

出力

Bytes = 200 
+0

あなたのプログラムを理解していれば、私はファイルでシリアル化する必要はありませんが、byteOutputではどうなりますか? – Mehdi

+1

はい、ディスクに書き込む他の理由がない限り、byteOutputにシリアライズできます。 – Adam

+0

ありがとう助けてくれてありがとう:) ちょうど最後の質問:もしbyteOutputがディスクにwrinteしないなら、それは書き込みますか? – Mehdi