Re: [OT] how to calculate the size of an object

2004-07-09 Thread Erik Weber
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 1
size of map = 417.955078125 kb
java SerTest 10
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:[EMAIL PROTECTED]
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: [EMAIL PROTECTED]
For 

Re: [OT] how to calculate the size of an object

2004-07-09 Thread Kris Schneider
Here's a concrete example. First, the Java class:

public class JvmtiSize {

public static native long getObjectSize(Object o);

public static void main(String[] args) {
String s = Hello, world!;
String[] sa = { s };

System.out.printf(s: %6d bytes\n, getObjectSize(s));
System.out.printf(   sa: %6d bytes\n, getObjectSize(sa));
System.out.printf(class: %6d bytes\n,
getObjectSize(JvmtiSize.class));
}
}

Then, run javah to generate JvmtiSize.h (omitted). Next, the C code
(jvmti_size.c):

#include jvmti.h
#include JvmtiSize.h

JavaVM *global_jvm = NULL;

JNIEXPORT jint JNICALL
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
  global_jvm = jvm;
  return 0;
}

JNIEXPORT jlong JNICALL
Java_JvmtiSize_getObjectSize(JNIEnv *env, jclass class, jobject object) {
  jint   status;
  jvmtiEnv  *jvmti;
  jvmtiError err;
  jlong  object_size;

  status = (*global_jvm)-GetEnv(global_jvm, (void **)jvmti, JVMTI_VERSION);
  if (status != JNI_OK) {
fprintf(stderr,
ERROR: Unable to create jvmtiEnv (GetEnv failed), error=%d\n,
status);
exit(1);
  }

  err = (*jvmti)-GetObjectSize(jvmti, object,  object_size);
  if (err != JVMTI_ERROR_NONE) {
fprintf(stderr,
ERROR: GetObjectSize failed, error=%d\n,
err);
exit(1);
  }

  return object_size;
}

Then, generate the shared lib (Solaris SPARC):

cc -G -I${JAVA_HOME}/include -Isrc -I${JAVA_HOME}/include/solaris
src/jvmti_size.c -o libjvmti_size.so

Finally, give it a run:

java -agentlib:jvmti_size -cp classes JvmtiSize
s: 24 bytes
   sa: 16 bytes
class:288 bytes

Quoting Kris Schneider [EMAIL PROTECTED]:

 Not too surprising that it's available through a native programming
 interface
 (JVMTI) since it's really an implementation-dependent metric:
 
 http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#GetObjectSize
 
 Quoting Brian Lee [EMAIL PROTECTED]:
 
  If you run this from a simple console test app, the JVM won't allocate any
 
  extra objects between 2 and 4.
  
  Unfortunatly, this is the most exact way to find out memory usage 
  (serialization size doesn't necessarily mean in memory size).
  
  Just wait til those slackers at Sun at a Object.sizeof() method in jdk1.9
 or
  
  something lame.
  
  BAL
  
  From: Navjot Singh [EMAIL PROTECTED]
  To: Struts Users Mailing List [EMAIL PROTECTED]
  Subject: Re: [OT] how to calculate the size of an object
  Date: Thu, 08 Jul 2004 22:16:12 +0530
  
  hi,
  
  Thanks for the link but this is very naive way of doing it. I am leaving
 it
  
  to the mercy of gc.
  
  What this method is doing
  
  1. run gc() manually (AND hope it wont run automatically again soon.)
  2. free memory
  3. create and object.
  4. free memory
  
  and now just wish that JVM wont allocate any memory in it's heap between 
  steps 2  4. so that one can assume that whatsoever output comes belongs
 to
  
  my object. I am at something better.
  
  Jim you are absolutely right, this technique may return a negative
 number.
  
  navjot singh
  
  
  [EMAIL PROTECTED] wrote:
  
  
  http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object
  
  The first one looks promising.
  
  Dennis
  
  
  
  
  *Navjot Singh [EMAIL PROTECTED]*
  
  07/08/2004 11:57 AM
  Please respond to
  Struts Users Mailing List [EMAIL PROTECTED]
  
  
  
  To
Struts Users Mailing List [EMAIL PROTECTED]
  cc
  
  Subject
[OT] how to calculate the size of an object
  
  
  
  
  
  
  
  
  hi,
  
  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;
  }
  
  --
  regards
  Navjot Singh
  
  When you jump for joy, beware that no-one moves the ground from beneath
  your feet. -- Stanislaw Lem, Unkempt Thoughts
  
  -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  
  
  
  
  
  -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  
  --
  regards
  Navjot Singh
  
  When you jump for joy, beware that no-one moves the ground from beneath
  your feet. -- Stanislaw Lem, Unkempt Thoughts
 
 -- 
 Kris Schneider mailto:[EMAIL PROTECTED]
 D.O.Tech   http://www.dotech.com/

-- 
Kris Schneider mailto:[EMAIL PROTECTED]
D.O.Tech   http://www.dotech.com/


[OT] how to calculate the size of an object

2004-07-08 Thread Navjot Singh
hi,
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;
}
--
regards
Navjot Singh
When you jump for joy, beware that no-one moves the ground from beneath
your feet. -- Stanislaw Lem, Unkempt Thoughts
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [OT] how to calculate the size of an object

2004-07-08 Thread John Moore
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
--
==
John Moore - Norwich, UK - [EMAIL PROTECTED]
==
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: [OT] how to calculate the size of an object

2004-07-08 Thread Jim Barrows
java.io.File has the length() method to return the length of the file.

 -Original Message-
 From: Navjot Singh [mailto:[EMAIL PROTECTED]
 Sent: Thursday, July 08, 2004 8:58 AM
 To: Struts Users Mailing List
 Subject: [OT] how to calculate the size of an object
 
 
 hi,
 
 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;
 }
 
 -- 
 regards
 Navjot Singh
 
 When you jump for joy, beware that no-one moves the ground 
 from beneath
 your feet. -- Stanislaw Lem, Unkempt Thoughts
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: [OT] how to calculate the size of an object

2004-07-08 Thread DGraham

http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object

The first one looks promising. 

Dennis







Navjot Singh [EMAIL PROTECTED]

07/08/2004 11:57 AM



Please respond to
Struts Users Mailing List [EMAIL PROTECTED]





To
Struts Users Mailing List
[EMAIL PROTECTED]


cc



Subject
[OT] how to calculate the
size of an object








hi,

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

-- 
regards
Navjot Singh

When you jump for joy, beware that no-one moves the ground from beneath
your feet. -- Stanislaw Lem, Unkempt Thoughts

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

RE: [OT] how to calculate the size of an object

2004-07-08 Thread Jim Barrows
Only problem is, that this is the size of the object with a new object, rather then 
being something that could be done dynamically.  Not to mention
that if this is done at run time ( which is what the original poster wants I believe) 
then you can be completely screwed by a GC run, and end up 
with a negative number.
 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 08, 2004 9:10 AM
To: Struts Users Mailing List
Subject: Re: [OT] how to calculate the size of an object



http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object 

The first one looks promising. 

Dennis 





Navjot Singh [EMAIL PROTECTED] 


07/08/2004 11:57 AM 


Please respond to
Struts Users Mailing List [EMAIL PROTECTED]



To
Struts Users Mailing List [EMAIL PROTECTED] 

cc

Subject
[OT] how to calculate the size of an object






hi,

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

-- 
regards
Navjot Singh

When you jump for joy, beware that no-one moves the ground from beneath
your feet. -- Stanislaw Lem, Unkempt Thoughts

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






RE: [OT] how to calculate the size of an object

2004-07-08 Thread Jim Barrows
Whoops.. that should the problem with the first one on the list.. :)

I know Bad code monkey no caffeine.

 -Original Message-
 From: Jim Barrows 
 Sent: Thursday, July 08, 2004 9:24 AM
 To: Struts Users Mailing List
 Subject: RE: [OT] how to calculate the size of an object
 
 
 Only problem is, that this is the size of the object with a 
 new object, rather then being something that could be done 
 dynamically.  Not to mention
 that if this is done at run time ( which is what the original 
 poster wants I believe) then you can be completely screwed by 
 a GC run, and end up 
 with a negative number.
  
 
 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED]
 Sent: Thursday, July 08, 2004 9:10 AM
 To: Struts Users Mailing List
 Subject: Re: [OT] how to calculate the size of an object
 
 
 
 http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object 
 
 The first one looks promising. 
 
 Dennis 
 
 
 
 
 
 Navjot Singh [EMAIL PROTECTED] 
 
 
 07/08/2004 11:57 AM 
 
 
 Please respond to
 Struts Users Mailing List [EMAIL PROTECTED]
 
 
 
 To
 Struts Users Mailing List [EMAIL PROTECTED] 
 
 cc
 
 Subject
 [OT] how to calculate the size of an object
 
   
 
 
 
 
 hi,
 
 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;
 }
 
 -- 
 regards
 Navjot Singh
 
 When you jump for joy, beware that no-one moves the ground 
 from beneath
 your feet. -- Stanislaw Lem, Unkempt Thoughts
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 
 
 
 

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: [OT] how to calculate the size of an object

2004-07-08 Thread Jim Barrows


 -Original Message-
 From: Navjot Singh [mailto:[EMAIL PROTECTED]
 Sent: Thursday, July 08, 2004 9:46 AM
 To: Struts Users Mailing List
 Subject: Re: [OT] how to calculate the size of an object
 
 
 hi,
 
 Thanks for the link but this is very naive way of doing it. I 
 am leaving 
 it to the mercy of gc.

Look at some of the other articles.. you'll find some better articles there on how to 
do it.

 
 What this method is doing
 
 1. run gc() manually (AND hope it wont run automatically again soon.)
 2. free memory
 3. create and object.
 4. free memory
 
 and now just wish that JVM wont allocate any memory in it's 
 heap between 
 steps 2  4. so that one can assume that whatsoever output 
 comes belongs 
 to my object. I am at something better.
 
 Jim you are absolutely right, this technique may return a 
 negative number.
 
 navjot singh
 
 
 [EMAIL PROTECTED] wrote:
 
  
  http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object
  
  The first one looks promising.
  
  Dennis
  
  
  
  
  *Navjot Singh [EMAIL PROTECTED]*
  
  07/08/2004 11:57 AM
  Please respond to
  Struts Users Mailing List [EMAIL PROTECTED]
  
  
  
  To
  Struts Users Mailing List [EMAIL PROTECTED]
  cc
  
  Subject
  [OT] how to calculate the size of an object
  
  
  
  
  
  
  
  
  hi,
  
  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;
  }
  
  -- 
  regards
  Navjot Singh
  
  When you jump for joy, beware that no-one moves the ground 
 from beneath
  your feet. -- Stanislaw Lem, Unkempt Thoughts
  
  
 -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  
  
  
  
 --
 --
  
  
 -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
 
 -- 
 regards
 Navjot Singh
 
 When you jump for joy, beware that no-one moves the ground 
 from beneath
 your feet. -- Stanislaw Lem, Unkempt Thoughts
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: [OT] how to calculate the size of an object

2004-07-08 Thread Erik Weber
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [OT] how to calculate the size of an object

2004-07-08 Thread Erik Weber
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [OT] how to calculate the size of an object

2004-07-08 Thread Brian Lee
If you run this from a simple console test app, the JVM won't allocate any 
extra objects between 2 and 4.

Unfortunatly, this is the most exact way to find out memory usage 
(serialization size doesn't necessarily mean in memory size).

Just wait til those slackers at Sun at a Object.sizeof() method in jdk1.9 or 
something lame.

BAL
From: Navjot Singh [EMAIL PROTECTED]
To: Struts Users Mailing List [EMAIL PROTECTED]
Subject: Re: [OT] how to calculate the size of an object
Date: Thu, 08 Jul 2004 22:16:12 +0530
hi,
Thanks for the link but this is very naive way of doing it. I am leaving it 
to the mercy of gc.

What this method is doing
1. run gc() manually (AND hope it wont run automatically again soon.)
2. free memory
3. create and object.
4. free memory
and now just wish that JVM wont allocate any memory in it's heap between 
steps 2  4. so that one can assume that whatsoever output comes belongs to 
my object. I am at something better.

Jim you are absolutely right, this technique may return a negative number.
navjot singh
[EMAIL PROTECTED] wrote:
http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object
The first one looks promising.
Dennis

*Navjot Singh [EMAIL PROTECTED]*
07/08/2004 11:57 AM
Please respond to
Struts Users Mailing List [EMAIL PROTECTED]

To
Struts Users Mailing List [EMAIL PROTECTED]
cc
Subject
[OT] how to calculate the size of an object



hi,
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;
}
--
regards
Navjot Singh
When you jump for joy, beware that no-one moves the ground from beneath
your feet. -- Stanislaw Lem, Unkempt Thoughts
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
regards
Navjot Singh
When you jump for joy, beware that no-one moves the ground from beneath
your feet. -- Stanislaw Lem, Unkempt Thoughts
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [OT] how to calculate the size of an object

2004-07-08 Thread Kris Schneider
Not too surprising that it's available through a native programming interface
(JVMTI) since it's really an implementation-dependent metric:

http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#GetObjectSize

Quoting Brian Lee [EMAIL PROTECTED]:

 If you run this from a simple console test app, the JVM won't allocate any 
 extra objects between 2 and 4.
 
 Unfortunatly, this is the most exact way to find out memory usage 
 (serialization size doesn't necessarily mean in memory size).
 
 Just wait til those slackers at Sun at a Object.sizeof() method in jdk1.9 or
 
 something lame.
 
 BAL
 
 From: Navjot Singh [EMAIL PROTECTED]
 To: Struts Users Mailing List [EMAIL PROTECTED]
 Subject: Re: [OT] how to calculate the size of an object
 Date: Thu, 08 Jul 2004 22:16:12 +0530
 
 hi,
 
 Thanks for the link but this is very naive way of doing it. I am leaving it
 
 to the mercy of gc.
 
 What this method is doing
 
 1. run gc() manually (AND hope it wont run automatically again soon.)
 2. free memory
 3. create and object.
 4. free memory
 
 and now just wish that JVM wont allocate any memory in it's heap between 
 steps 2  4. so that one can assume that whatsoever output comes belongs to
 
 my object. I am at something better.
 
 Jim you are absolutely right, this technique may return a negative number.
 
 navjot singh
 
 
 [EMAIL PROTECTED] wrote:
 
 
 http://www.google.com/search?hl=enlr=ie=UTF-8q=size+java+object
 
 The first one looks promising.
 
 Dennis
 
 
 
 
 *Navjot Singh [EMAIL PROTECTED]*
 
 07/08/2004 11:57 AM
 Please respond to
 Struts Users Mailing List [EMAIL PROTECTED]
 
 
 
 To
 Struts Users Mailing List [EMAIL PROTECTED]
 cc
 
 Subject
 [OT] how to calculate the size of an object
 
 
 
 
 
 
 
 
 hi,
 
 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;
 }
 
 --
 regards
 Navjot Singh
 
 When you jump for joy, beware that no-one moves the ground from beneath
 your feet. -- Stanislaw Lem, Unkempt Thoughts
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 
 
 
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 --
 regards
 Navjot Singh
 
 When you jump for joy, beware that no-one moves the ground from beneath
 your feet. -- Stanislaw Lem, Unkempt Thoughts

-- 
Kris Schneider mailto:[EMAIL PROTECTED]
D.O.Tech   http://www.dotech.com/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: [OT] how to calculate the size of an object

2004-07-08 Thread simarjit singh
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



-Original Message-
From: Erik Weber [mailto:[EMAIL PROTECTED]
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: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]