Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

时光毁灭记忆、已成空白 提交于 2020-01-04 11:03:53

问题


Each slot of local variable array is of 4-bytes. So to store a character, short or byte variables one slot is used. Means all smaller data-types internally gets converted into int data type.

My doubt is:

1). Is it not making smaller data types useless, if internally they are of 4-bytes?, If yes, Why not remove such data-types from the language? 2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte?


回答1:


1). Is it not making smaller data types useless, if internally they are of 4-bytes?

When it comes to local variables or arithmetic, then yes, using smaller data types can be useless and even waste performance due to required value set conversions.

However, besides local variables, there’s a heap and for fields and arrays it can make (implementation specific) differences.

If yes, Why not remove such data-types from the language?

First, as said above, there’s the heap. Second, there’s I/O. You are exchanging data with the outside world in term s of bytes or char’s or, nowadays, codepoints. So besides storage sizes, these data types carry semantics. It makes a huge difference whether you invoke StringBuilder.append(char) or StringBuilder.append(byte), though in both cases, an int value is actually passed.

2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte?

If each slot is one byte, it would be impossible to store anything else into it. You are confusing stack frame slots (used for local variables and the operand stack) with actual storage. A slot is intended to hold a value of an arbitrary data type and may be mapped to an actual storage or a CPU register in an implementation-dependent way. The fact that long and double consume two slots is a historical compromise, stemming from the fact the Java was designed in the early nineties of the last century.

Had it designed today, you can bet, a slot would be defined to be able to hold all data types, including long and double. The key point here is simplification of the architecture. If there is a potential benefit from using smaller data types for a local variable or an operand for a particular underlying architecture, it’s up to the JVM implementation to detect this and generate the appropriate native code. As said, in most cases, due to the local variables and operand stack entries being mapped to CPU registers, there is no such benefit.




回答2:


Is it not making smaller data types useless, if internally they are of 4-bytes?,

Registers are always 32-bit or 64-bit depending on the architecture. Never the less when you store lots of values in memory, using 1 byte, 2 bytes or 4 bytes adds up.

If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte?

One byte can only store 256 values and it can't store a wider range. I think you mean; why isn't everything a multiple of 1 byte? The doesn't matter because the memory usage is virtual. The actual memory usage can be optimised.




回答3:


Local variable are stored in 4 bytes (byte, char, short, int, boolean, float, reference to objects) or 8 bytes (long, double).

But variables are not needed only locally to a function. Serialization can use the smallest amount of bytes.

There is a mix between speed performances and space occupancy.

Accessing a 4 byte variable is faster because modern CPU use registers of 32 or 64 bits, so any operation to extract a smaller amount of bytes from a block of 4 is a waste of time for cpu.




回答4:


We can't remove bytes from language bacause of arrays. Array of byte's four time smaller than array of int's.

And ther is no reason to make variable slot 1-byte because it will not save much memory (stack is usually much smaller than heap) and memory work faster if everithing is padded.



来源:https://stackoverflow.com/questions/39226918/why-each-slot-of-the-local-variable-array-in-a-stack-frame-is-of-4-bytes-and-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!