struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Weber <erikwe...@mindspring.com>
Subject Re: [OT] how to calculate the size of an object
Date Fri, 09 Jul 2004 16:11:30 GMT
I wrote a simple test.

VO.java is a simple value object class with a couple of Strings and an 
int as instance fields.

SerTest.java creates a number of instances of VO, inserts them into a 
HashMap, and then serializes the entire map to a ByteArrayOutputStream. 
Finally it prints the length of the resulting byte array in KB to 
standard out.

You specify how many VO instances to put in the HashMap with args[0]. 
Notice that each VO's "name" field gets a new String object -- not a 
shared one.

Here are some results. I notice a pattern:

java SerTest 1
size of map = 0.2373046875 kb

java SerTest 10
size of map = 0.5625 kb

java SerTest 100
size of map = 3.990234375 kb

java SerTest 1000
size of map = 40.025390625 kb

java SerTest 10000
size of map = 417.955078125 kb

java SerTest 100000
size of map = 4373.033203125 kb


simarjit singh wrote:

>With ByteArrayOutputStream approach, writeObject Method is also serializing
>the object. Ofcourse serialization doesn't give size as it is more of a meta
>data for the entire web of objects associated with the serialized object.
>
>Rather than writing serialized object on file we  are putting it in a byte
>array stream.
>
>What u are getting is NOT bytes for object but bytes for serliazed data
>
>
>Correct me if i am wrong...
>
>-Simar
>  
>

You are right, serialization includes "meta data", but, the "bytes for 
serialized data" certainly *contains* the "bytes for object"! So, while 
the *absolute* accuracy of this data may be somewhat suspect 
(serialization overhead needs to be subtracted, for one), the point is 
that it is more than likely useful and probably gives a good ballpark 
idea of how big an object is. As I understand it, serialization overhead 
represents only a fraction of those bytes.

It would be interesting to see a comparison using the other method of 
determining memory use and see how it comes out!

I'll tell you who would be a good authority on this subject, is Bill 
Venners over at Artima.com.

Good luck!

Erik



import java.util.HashMap;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;

public class SerTest {

  public static void main(String[] args) throws Exception {
	int count = Integer.parseInt(args[0]);
	HashMap map = new HashMap();
	for (int x = 0; x < count; x++) {
	  map.put(new Integer(x), new VO(new String("John Doe " + x), String.valueOf(x), (int) (Math.random()
* 100)));
	}
	printSize(map);
  }

  public static void printSize(Object o) throws Exception {
	ByteArrayOutputStream bs = new ByteArrayOutputStream();
	ObjectOutputStream out = new ObjectOutputStream(bs);
	out.writeObject(o);
	out.close();
	double size = bs.toByteArray().length;
	System.out.println("size of map = " + ((double) (size / 1024D)) + " kb"); 
  }

}


import java.io.Serializable;

public class VO implements Serializable {

  protected String name;
  protected String ID;
  protected int age;

  public VO(String name, String ID, int age) {
	this.name = name;
	this.ID = ID;
	this.age = age;
  }

  public void setName(String name) {
	this.name = name;
  }

  public String getName() {
	return name;
  }

  public void setID(String ID) {
	this.ID = ID;
  }

  public String getID() {
	return ID;
  }

  public void setAge(int age) {
	this.age = age;
  }

  public int getAge() {
	return age;
  }

}





>
>
>-----Original Message-----
>From: Erik Weber [mailto:erikweber@mindspring.com]
>Sent: Thursday, July 08, 2004 10:57 PM
>To: Struts Users Mailing List
>Subject: Re: [OT] how to calculate the size of an object
>
>
>Also remember that when you serialize an Object (if I am not mistaken),
>all the Objects referred to by that Object get serialized too (unless
>the references are transient). In addition, there is serialization
>overhead (protocol info that is not actually part of your Object but
>that is required to deserialize). Your returned size could be misleading
>if you have references to, say, some "parent" Object(s) in LDIFData, or
>a ton of serialization overhead (unlikely).
>
>Erik
>
>
>
>Erik Weber wrote:
>
>  
>
>>public static long getSize(LDIFData data) {
>>try {
>>ByteArrayOutputStream baos = new ByteArrayOutputStream();
>>ObjectOutputStream out = new ObjectOutputStream(baos);
>>out.writeObject(data);
>>out.close();
>>return baos.toByteArray().length;
>>}
>>catch (Exception e) {
>>e.printStackTrace();
>>return -1;
>>}
>>}
>>
>>John Moore wrote:
>>
>>    
>>
>>>Navjot Singh wrote:
>>>
>>>      
>>>
>>>>I use SAX parser to load an LDIF file into memory. Whatsoever data i
>>>>read, i fill into an object.
>>>>
>>>>I need to know *the size of LDIFData object* at runtime. How to do
>>>>that?
>>>>
>>>>Well the class structure is something like this
>>>>
>>>>public class LDIFData{
>>>>ArrayList cards; // collection of Card
>>>>String filename;
>>>>long lastLoadedTime;
>>>>}
>>>>
>>>>public class Card{
>>>>String name;
>>>>String email
>>>>String mobile;
>>>>}
>>>>
>>>>        
>>>>
>>>Off the top of my head...
>>>
>>>Serialize it to a byte array output stream, see how many bytes you have
>>>
>>>John
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message