Possible Duplicate:
In Java, what is the best way to determine the size of an object?
In Actionscript I can usually just do:
var myVar:uint = 5;
getSize(myVar)
//outputs 4 bytes
How do I do this in Java?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you turn off
-XX:-UseTLAByou can check the Runtime.freeMemory() before and after. However in the case of local variables, they don’t take space on the heap (as they use the stack) and you can’t get it size.However, an
intis a 32-bit sign value and you can expect it will use 4-bytes (or more depending on the JVM and the stack alignment etc)The
sizeofin C++ is useful for pointer arithmetic. Since Java doesn’t allow this, its isn’t useful and possibly deliberately hidden to avoid developers worrying about low level details.