import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class ObjectUtils {
public static int getObjectSize(Object o) {
ObjectOutput out = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
out = new ObjectOutputStream(bos);
out.writeObject(o);
out.flush();
byte[] b = bos.toByteArray();
return b.length;
} catch (IOException e) {
return -1;
}
}
}
public static void main(String[] args) {
{
String o = "ABC";
System.err.println(ObjectUtils.getObjectSize(o));
}
{
Integer o = 100;
System.err.println(ObjectUtils.getObjectSize(o));
}
{
String[] o = { "ABC", "ABC", "ABC" };
System.err.println(ObjectUtils.getObjectSize(o));
}
}
10
81
60
Recommended Posts