我想在此實例中且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();
}
}
}
}
再次問同樣的事情不會幫助,你知道... http://stackoverflow.com/questions/9789045 /內存佔用後反序列化 – skaffman 2012-03-20 16:50:08
不,它不一樣,在這裏我問如何計算簡單對象的內存!並在序列化後的最後一個問題!!!!!!!!!! – Mehdi 2012-03-20 16:53:53
可能重複[在Java中對象的內存消耗是多少?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java) – dty 2012-03-20 16:58:56