Memory Usage in Java

Kenneth Fogel kfogel at dawsoncollege.qc.ca
Fri Aug 8 19:35:18 UTC 2025


First, thank you to those who replied and to those who may still reply. Let me apologize for neglecting the last 25+ years of hardware design.

>From what you have written or had on your blog, the issue of how much RAM a data type consumes, is not significant, at least to someone just starting their programming career. Back in my C/C++ days in the 1990s I worked on the assumption that for performance reasons your preferred data types were  the word size of your CPU, like an int, because, and it could likely be because I misunderstood what was happening in the hardware, a program ran faster if you used, say, an int.

I had not planned to get into these details on what I am working on but this morning I thought there might be some value to knowing precisely how primitive types consume memory. I think I will just stick with explaining what a stack and a heap are. I don't think someone learning Java, it is a textbook I am writing,  would gain any insight into coding knowing how data is laid out in RAM. Correct me if I am wrong.

Thanks again,

Ken


From: discuss <discuss-retn at openjdk.org<mailto:discuss-retn at openjdk.org>> On Behalf Of Kenneth Fogel
Sent: August 8, 2025 12:11 PM
To: discuss at openjdk.org<mailto:discuss at openjdk.org>
Subject: Memory Usage in Java

Please let me know where I might ask this question or find the answer if this is not the right place.

In a book I am working on I am writing about how Java deals with memory allocation for the primitive types on a 64 bit CPU. I know the size of primitive variable types such as byte is 1, int is 4, etc. and that the size is related to the range of allowable values. From my C/C++ days I learned that the actual amount of RAM used was related to the word size of a CPU so that when a byte is read you are really reading a word. This meant to me that if I had 10 variables of type byte, not in an array, it would consume 80 bytes of RAM. From my reading it appears to be the same in Java except for arrays. Is this accurate? For example, an int is 4 bytes but when Java runs on a 64 bit CPU is it really reading and writing 8 bytes? If it is, can we say that the 4 bytes of padding is RAM dead space that cannot be used for anything else?

Thank you for your time,

Ken Fogel




From: discuss <discuss-retn at openjdk.org> On Behalf Of Kenneth Fogel
Sent: August 8, 2025 12:11 PM
To: discuss at openjdk.org
Subject: Memory Usage in Java

Please let me know where I might ask this question or find the answer if this is not the right place.

In a book I am working on I am writing about how Java deals with memory allocation for the primitive types on a 64 bit CPU. I know the size of primitive variable types such as byte is 1, int is 4, etc. and that the size is related to the range of allowable values. From my C/C++ days I learned that the actual amount of RAM used was related to the word size of a CPU so that when a byte is read you are really reading a word. This meant to me that if I had 10 variables of type byte, not in an array, it would consume 80 bytes of RAM. From my reading it appears to be the same in Java except for arrays. Is this accurate? For example, an int is 4 bytes but when Java runs on a 64 bit CPU is it really reading and writing 8 bytes? If it is, can we say that the 4 bytes of padding is RAM dead space that cannot be used for anything else?

Thank you for your time,

Ken Fogel


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20250808/318aee6e/attachment-0001.htm>


More information about the discuss mailing list