开发者

How much space does "variable name" takes?

I have 2 variable like below

char s='s'; 
char Kaazoooombaa ='s';

how can I know which variable is consuming 开发者_运维问答how much space ?


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.


Variable names mean nothing after compilation; there are only there to make the program easy for humans to read and write.


Variable names are for human readability. In this case s and Ka..a are the same size.

@TomTom for things like public their names again don't change size but the fact that they are public does if I'm not mistaken. That size may be taken on by the class which contains them though.


Generally variable names is used by the compiler, not the runtime. If the class are exported from your module, then the names are encoded in the metadata.

But if you are worried how much space your program will take, then the name of variables is the very least of you worries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜