Can you tell me the size of the memory that a variable occupies in java?

Asked 2 years ago, Updated 2 years ago, 30 views

I'd like to compare the memory usage of the raw type variable and the refer type variable. For example:

short a = 1;
Short b = 1;

I want to know how much difference a and b have in memory usage. Instance variables, class (static) variables, and local variables in the method.

Is there a way?

java

2022-09-22 16:53

1 Answers

Both use 4 bytes.

There is a place called local variable section where regional variables of the method in the runtime area in jvm are stored.

It is stored in 4-byte increments and uses 8 bytes for large types such as long or double (as you can see from Java's data type, there is nothing larger than 8 bytes for primitive types.)

Boolean also uses 4 bytes. And in jvm, there's no boolean type, just int 0 or 1.

Of course, I use 4 bytes of reference type.


2022-09-22 16:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.