Memory Usage in Java

Dallman, John john.dallman at siemens.com
Fri Aug 8 16:48:44 UTC 2025


Hi, Kenneth,

> 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.

I'm afraid you are confused about that, too. There have been computers that would do that, but they have been obsolete for decades. I'm pretty sure Java has never run on a "word addressed" machine.

On any remotely modern processor architecture, ten byte-sized variables will only occupy ten bytes of space. The way they get fetched into the processor is somewhat complicated but ensures that several of them will normally be fetched in one read.

With best regards,

--
John Dallman (he/him)

Siemens Industry Software Limited
DI SW PLM PE OT PC PDE
Kett House, Station Road,
Cambridge CB1 2JH, United Kingdom
mailto:john.dallman at siemens.com
www.sw.siemens.com<http://www.sw.siemens.com/>
[Logo]
Siemens Industry Software Limited registered office: Pinehurst 2, Pinehurst Road, Farnborough, Hampshire, GU14 7BF, United Kingdom. Registered in England and Wales No. 03476850.

This communication contains information which is confidential and may also be privileged. It is for the exclusive use of the addressee. If you are not the addressee please note that any distribution, reproduction, copying, publication or use of this communication or the information is prohibited. If you have received this communication in error, please contact us immediately and also delete the communication from your computer.

From: discuss <discuss-retn at openjdk.org> On Behalf Of Shipilev, Aleksey
Sent: 08 August 2025 17:35
To: Kenneth Fogel <kfogel at dawsoncollege.qc.ca>; discuss at openjdk.org
Subject: Re: Memory Usage in Java

Hi Kenneth,

The answers to most of these questions are, "No, this is not how it works in any modern JVM".

I would really recommend playing with JOL, especially look through its Samples:
https://github.com/openjdk/jol
https://github.com/openjdk/jol/tree/master/jol-samples/src/main/java/org/openjdk/jol/samples

And maybe read this for even more discussion:
https://shipilev.net/jvm/objects-inside-out/

Thanks,
-Aleksey

From: discuss <discuss-retn at openjdk.org<mailto:discuss-retn at openjdk.org>> on behalf of Kenneth Fogel <kfogel at dawsoncollege.qc.ca<mailto:kfogel at dawsoncollege.qc.ca>>
Date: Friday, 8. August 2025 at 18:11
To: "discuss at openjdk.org<mailto:discuss at openjdk.org>" <discuss at openjdk.org<mailto:discuss at openjdk.org>>
Subject: [EXTERNAL] Memory Usage in Java


CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

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





Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20250808/16291f7a/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 2730 bytes
Desc: image001.gif
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20250808/16291f7a/image001-0001.gif>


More information about the discuss mailing list