473,508 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to measure the size of an object?

People, how can I know how big an object is? I mean, I have an object the
collects data from a stream and when it grows to an especific size I need
to create a new object to continue collecting the data and send the other
one to a thread that records it's content to the disk.

How do I do such thing?

TIA,
Scirious.
May 26 '06 #1
6 14312

"Scirious" <sc******@scirious.com> wrote in message
news:1_******************************@giganews.com ...
People, how can I know how big an object is? I mean, I have an object the
collects data from a stream and when it grows to an especific size I need
to create a new object to continue collecting the data and send the other
one to a thread that records it's content to the disk.

How do I do such thing?


Are you sure it's the object's size you need, and not, for example, the
size of the buffer within that object?

How about whenever you collect a unit of data, you increment a counter?

- Oliver

May 26 '06 #2
>
Are you sure it's the object's size you need, and not, for
example, the
size of the buffer within that object?

How about whenever you collect a unit of data, you increment a
counter?

- Oliver


Actually, I'm not! The thing is, I've already seen the JVM misbehave when
it uses a lot of memmory. Lets say, for example, If I have 750 MB of RAM
and an object needs to store 1.5+ GB of data my system or the JVM is
likely to crash. So, for example, I'm writing an e-mail client and I have
an object to store the headers I download from the server. I may want to
specify that, after downloading 250 MB of headers, I want them to be
recorded on disc so the object can give place to a new one and be collected
from memory.

This way I avoid the possibility of an object getting as big as 1 GB. But
to do so I need a way to measure the size it has and, implementing a
counter is not the best way sisnce the size of the headers may differ from
message to message.

Scirious.
May 29 '06 #3
[Crossposted to comp.lang.java.programmer, since more people lurk there]

"Scirious" <sc******@scirious.com> wrote in message
news:lr********************@giganews.com...
People, how can I know how big an object is? I mean, I have an object
the
collects data from a stream and when it grows to an especific size I
need
to create a new object to continue collecting the data and send the
other
one to a thread that records it's content to the disk.

How do I do such thing?

I wrote:
Are you sure it's the object's size you need, and not, for
example, the
size of the buffer within that object?

How about whenever you collect a unit of data, you increment a
counter?

OP responds:
Actually, I'm not! The thing is, I've already seen the JVM misbehave when
it uses a lot of memmory. Lets say, for example, If I have 750 MB of RAM
and an object needs to store 1.5+ GB of data my system or the JVM is
likely to crash. So, for example, I'm writing an e-mail client and I have
an object to store the headers I download from the server. I may want to
specify that, after downloading 250 MB of headers, I want them to be
recorded on disc so the object can give place to a new one and be
collected
from memory.

This way I avoid the possibility of an object getting as big as 1 GB. But
to do so I need a way to measure the size it has and, implementing a
counter is not the best way sisnce the size of the headers may differ from
message to message.


Not sure the best way to solve this one, which is why I'm crossposting
it to c.l.j.programmer, so others can contribute.

I've read about one technique but never tried it: Allocate a big chunk
of memory at the start of your program, do your memory operation which might
result in an OutOfMemoryException (OOME), catch it, release the big chunk of
memory that you got, so you now have some wiggle room to work with,
serialize the buffer to disk, re-allocate another big chunk of memory (and
assert that it doesn't fail, since you're supposed to have a lot of free
memory after the serialization), and repeat.

- Oliver

May 29 '06 #4
Oliver Wong wrote:
[Crossposted to comp.lang.java.programmer, since more people lurk there]

"Scirious" <sc******@scirious.com> wrote in message
news:lr********************@giganews.com...
People, how can I know how big an object is? I mean, I have an
object the
collects data from a stream and when it grows to an especific size I
need
to create a new object to continue collecting the data and send the
other
one to a thread that records it's content to the disk.

How do I do such thing?
I wrote:
Are you sure it's the object's size you need, and not, for
example, the
size of the buffer within that object?

How about whenever you collect a unit of data, you increment a
counter?


OP responds:

Actually, I'm not! The thing is, I've already seen the JVM misbehave when
it uses a lot of memmory. Lets say, for example, If I have 750 MB of RAM
and an object needs to store 1.5+ GB of data my system or the JVM is
likely to crash. So, for example, I'm writing an e-mail client and I have
an object to store the headers I download from the server. I may want to
specify that, after downloading 250 MB of headers, I want them to be
recorded on disc so the object can give place to a new one and be
collected
from memory.

This way I avoid the possibility of an object getting as big as 1 GB. But
to do so I need a way to measure the size it has and, implementing a
counter is not the best way sisnce the size of the headers may differ
from
message to message.


Not sure the best way to solve this one, which is why I'm
crossposting it to c.l.j.programmer, so others can contribute.

I've read about one technique but never tried it: Allocate a big
chunk of memory at the start of your program, do your memory operation
which might result in an OutOfMemoryException (OOME), catch it, release
the big chunk of memory that you got, so you now have some wiggle room
to work with, serialize the buffer to disk, re-allocate another big
chunk of memory (and assert that it doesn't fail, since you're supposed
to have a lot of free memory after the serialization), and repeat.


In this scenario I'd just count downloaded bytes (I guess this is what
you meant by "counter" above). As easy as that. With a limit of 250MB
some bytes overhead for reference to an object's class etc. are negligible.

And I think you also correctly pointed out that "the size of an object"
is a too fuzzy term to deal with. Strictly speaking, the size of an
object is 8 bytes + 4 * size of an object reference + size of any POD
data members (roughly, I'm not 100% sure about the figures). But then
again, for a String you would certainly also want to count the size of
the char array that holds the actual string data etc. Matters become
even more complicated when two strings share the same array, e.g. you
use this constructor: public String(String original). Does the char
array count for each of them? Or is half the length counted for each of
them? This becomes even more complex with collections and user defined
classes...

Kind regards

robert
May 29 '06 #5

"Robert Klemme" <bo******@gmx.net> wrote in message
news:4e*************@individual.net...
"Scirious" <sc******@scirious.com> wrote in message
So, for example, I'm writing an e-mail client and I have
an object to store the headers I download from the server. I may want to
specify that, after downloading 250 MB of headers, I want them to be
recorded on disc so the object can give place to a new one and be
collected
from memory.
[...] In this scenario I'd just count downloaded bytes (I guess this is what you
meant by "counter" above). As easy as that. With a limit of 250MB some
bytes overhead for reference to an object's class etc. are negligible.


I think it's a bit trickier than that. Let's say the downloaded bytes
are 0x48, 0x65, 0x6C, 0x6C, 0x6F. I.e. the string "Hello" if decoded using
ASCII. If the program then takes these 5 bytes and stores them as a String,
the JVM is likely to use UTF-16 internally, which will result in 10 bytes
being used (2 for each character).

If the downloaded bytes UTF-8 instead of ASCII, and some of the
characters were above codepoint 256, then the calculation is not simply a
matter of doubling the downloaded bytes. Some characters will have taken 1
byte to download, 2 bytes to store; others will have taken 2 bytes to
download, 2 bytes to store. Still others, 3 bytes to download, 2 bytes to
store; 3 bytes to download, 3 bytes to store, and so on with various other
combinations.

- Oliver

May 29 '06 #6
Oliver Wong wrote:

"Robert Klemme" <bo******@gmx.net> wrote in message
news:4e*************@individual.net...
"Scirious" <sc******@scirious.com> wrote in message So, for example, I'm writing an e-mail client and I have
an object to store the headers I download from the server. I may
want to
specify that, after downloading 250 MB of headers, I want them to be
recorded on disc so the object can give place to a new one and be
collected
from memory.

[...]
In this scenario I'd just count downloaded bytes (I guess this is what
you meant by "counter" above). As easy as that. With a limit of
250MB some bytes overhead for reference to an object's class etc. are
negligible.


I think it's a bit trickier than that. Let's say the downloaded bytes
are 0x48, 0x65, 0x6C, 0x6C, 0x6F. I.e. the string "Hello" if decoded
using ASCII. If the program then takes these 5 bytes and stores them as
a String, the JVM is likely to use UTF-16 internally, which will result
in 10 bytes being used (2 for each character).

If the downloaded bytes UTF-8 instead of ASCII, and some of the
characters were above codepoint 256, then the calculation is not simply
a matter of doubling the downloaded bytes. Some characters will have
taken 1 byte to download, 2 bytes to store; others will have taken 2
bytes to download, 2 bytes to store. Still others, 3 bytes to download,
2 bytes to store; 3 bytes to download, 3 bytes to store, and so on with
various other combinations.


Maybe I wasn't clear enough: I meant, ignore real sizes and just use the
bytecount as limit - alternatively, if the data is converted to chars
then use the char count as limit. I wouldn't bother to think about
actual sizes in bytes - the important part for me would be that there is
a *limit* to the size.

Kind regards

robert
May 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2136
by: Anand Ganesh | last post by:
Hi All, I have an image which is made to fit in a Window. The user can Zoom In or Zoom Out and Pan the image. These images are Engineering drawings which has been drawn to scale say 1 inch =...
2
12339
by: AlirezaH | last post by:
How to measure hard disk capacity?
1
5950
by: Frank Rizzo | last post by:
I am doing some memory profiling work and I am trying to figure out the minimum size of an object. The object has a bunch of variables that are other objects (hashtables, array lists, and...
2
441
by: Tom L | last post by:
How can I tell how much memory is being user for each user session?
5
1388
by: Joe Fallon | last post by:
Is there a good way to measure the size of a page delivered to the browser? Also, how do measure the size of Viewstate? I am just using View Source, saving the text file and looking at the number...
3
3404
by: Neagu, Adrian | last post by:
Hello everybody, I try to solve the following problem: I have a python program that takes a lot of memory (>hundred Mb). I made an improvement (I hope) and I want to measure the gain (if...
14
3359
by: asit dhal | last post by:
Can anyone tell how to measure the size of an array without use of sizeof operator ?
4
1384
by: Academia | last post by:
I need to create a Bitmap just big enough to display a string. Since I need a Graphic object to measure the string I create and dispose a Bitmap and a Graphic object only for that purpose. ...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.