473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How can i determine the size of the class ?

Hi all,

I want to determine the size of my class at runtime. In C i do this:

struct
{
int id;
char name[30];
} registry
void main()
{
int size = sizeof(registry);
}

How i can do this in Java ?

Jul 17 '05 #1
2 5973

Luiz Antonio Gomes Picanço wrote:
Hi all,

I want to determine the size of my class at runtime. In C i do this:

struct
{
int id;
char name[30];
} registry
void main()
{
int size = sizeof(registry);
}

How i can do this in Java ?


You can't. Except:
If your class is big, you might be able to use
Java.lang.Runtime.freeMemory() and Java.lang.Runtime.totalMemory() and
subtract one from the other as a rough approximation. That's the best
you can do, just before and just after instantiating the class.

Jul 17 '05 #2
You can implement Serializable in the class and then write an instance of
the class to a file. The size of the file will tell you how big the object
was when it was serialized. Keep in mind that this doesn't tell you the
maximum possible size. For instance, if you have Strings as state inside
your class, the size of them can vary greatly. Also keep in mind that any
objects stored inside the class (unless they're declared as transient) must
also implement Serializable.

See the javadocs for java.io.ObjectOutputStream.

"Luiz Antonio Gomes Picanço" <lu**@luizantonio.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all,

I want to determine the size of my class at runtime. In C i do this:

struct
{
int id;
char name[30];
} registry
void main()
{
int size = sizeof(registry);
}

How i can do this in Java ?

Jul 17 '05 #3

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

Similar topics

1
by: Timo | last post by:
All my font-sizes are set as relative sizes in CSS (large, medium, small, x-small, etc). Let's say something is set in CSS to be xx-large, but a visually impaired user wants it displayed even...
3
by: JK | last post by:
This is an easy one, but I'm a JS newbie. I want to write a function that determines input value. If 0 - 100 than will use input as % (i.e., 3 would become .03 and used for calculations);...
19
by: Lauren Quantrell | last post by:
In Windows XP display Properties/Appearance users can chose between: Windows XP Style or Windows Classic Style. Is there a way in VBA to determine the user's selection? The reason: Toolbars...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
2
by: NewsAccount | last post by:
Hi I'm trying to find a way to programmatically measure the size of the page sent down to the browser, can't seem to measure the length of what gets written to the HTML TextWriter in protected...
7
by: Doru Roman | last post by:
Hi, What is the fastest way to evaluate manually the result in this case: int a, b, c; a = 255; b = 122; c = a & b; The only way I know is transforming each number into the binary value...
10
by: Pep | last post by:
I have a problem I need to solve which looks like memory exhaustion. Is there a function that I can use in c++ to determine the size of the heap before and after a allocation? The C++ code is...
5
by: sunny | last post by:
Hi All Is there any way to determine stack and heap size, during runtime. i.e can we predict stack overflow. etc
12
by: garyusenet | last post by:
I want to write a program that lists all folders on my local hard drive in order of size, any ideas for how I might do this? Thankyou, Gary.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.