I have 2 variable like below
char s='s';
char Kaazoooombaa ='s';
how can I know which variable is consuming how much space ?
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.
Other than a small amount of space for the metadata (which will be allocated once in total, not once per instance) the name makes no difference1. So in the example given, each of those variables will take 2 bytes, possibly padded to 4 bytes depending on the other fields and the VM being used.
Note that even for reference types (including arrays) the space taken by the variable itself is fixed by its type (and the details of the VM) – the space taken by a reference to an empty string is the same as the space taken by a reference to a huge string.
1 There may be a cost per use of the variable – or perhaps per class using the variable. You’d have to consult the VM specification to check the exact binary representation. Even then that’s the representation in the class file, which may well not lead to increased memory use by the time it’s all JIT compiled etc. It’s almost certain to be irrelevant in the grander scheme of things anyway – the memory required by actual objects almost always dwarfs the memory required by the code itself.