11

I have read what-is-the-memory-consumption-of-an-object-in-java and what-is-the-memory-overhead-of-an-object-in-java.

But still I am in confusion.

  • What is memory overhead?? is it the padding?
  • What is JVM with compressed pointers? is it reference??
  • If 32-bit JVM is used then overhead will be less? Ofcourse yes.But is it because of padding?
  • So is it better to use always 32-bit JVM for memory efficiency or for performance?

Below image is from this link(page no.26)

In this image at starting itself they shown as 16 bytes JVM overhead,why that so??

enter image description here

Community
  • 1
  • 1
Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52
  • 1
    Regarding 32 vs 64 bit JVMs: see http://stackoverflow.com/questions/17285928/jre-32bit-vs-64bit – GhostCat Oct 12 '16 at 07:28
  • @GhostCat but if 32 bit is better for memory consumption..what is the reason for 64-bit jvm?? is it because 32-bit is limited to 4GB heap or the compressed oops?? – Prasanna Kumar H A Oct 12 '16 at 07:32
  • http://openjdk.java.net/projects/code-tools/jol/ – the8472 Oct 12 '16 at 07:55
  • 4 GB is nothing nowadays. So some people say; so yes, most people are just fine with the overhead that using 64 bit OS / application comes with. – GhostCat Oct 12 '16 at 07:57
  • @GhostCat overhead?? is it padding or 4bytes or 8 bytes in 64bit( **But** compressed oops reverts it to 4 bytes only(?)..then what is the difference in overhead on using 64bit) – Prasanna Kumar H A Oct 12 '16 at 07:59
  • @GhostCat have a 32 GB desktop, but I only use it for reading email etc. I remote into a 128 GB machine for serious development ;) My 8 yo has an old machine of mine with 24 GB (and two screens) – Peter Lawrey Oct 12 '16 at 08:30
  • 1
    @PeterLawrey Impressive. Your 8yo has more RAM at hand as my whole household. But still, I can top that: I had my fingers on a IBM Power systems 5 years back. Which had 400 GB RAM. And I was able to almost fully make use of that. – GhostCat Oct 12 '16 at 08:34
  • @GhostCat my first computer had 128 KB and in five years of programming it I was able to almost fully make use of it. ;) – Peter Lawrey Oct 12 '16 at 10:56
  • @PeterLawrey My first computer was an abacus! But glad to see that you keep responding with smileys. I saw some serial downvoting this morning; and now I am wondering which of the few "> 125 reputation download privilege" persons I had contact with ... might have a reason to get angry with me. But I see that we are good, so I guess I have to continue my search elsewhere. ;-) .... oh, how that sucks. – GhostCat Oct 12 '16 at 11:00

1 Answers1

12

What is memory overhead??

When more memory is used than the fields you created.

is it the padding?

Some is padding which can appear anywhere in the object, except the header which is always at the start. The header is typically 8-12 bytes long.

What is JVM with compressed pointers?

A technique for using 32-bit pointers in a 64-bit JVM to save memory.

is it reference??

References can use this technique but so can pointers to the class information for an object.

If 32-bit JVM is used then overhead will be less?

Possibly, though this is the same as using compressed pointers for references and classes.

But is it because of padding?

It's because 64-bit pointers use more space than 32-bit pointer.

So is it better to use always 32-bit JVM for memory efficiency or for performance?

No, the 32-bit processor model has 32-bit registers where as the 64-bit model has twice as many registers which are double the sized (64-bit) means far more can be held in the fastest memory, the registers. 64-bit calculations tend to be faster as well with a 64-bit processing model.

In general I would recommend you always use the 64-bit JVM unless you a) can't or b) have a very small amount of memory.

In this image at starting itself they shown as 16 bytes JVM overhead,why that so??

This is not strictly correct. This assumes you have a non compressed class reference so the header is 12-bytes, however objects are 8 byte aligned by default, which means there will be 4 bytes of padding at the end (which totals 16 bytes but it's not all at the start)

FAQ: Why can 32-bit Compressed OOP address more than 4 GB

Object have to be 8-byte aligned by default. This makes memory management easier but wastes some padding sometimes. A side effect is that the address of every object will have 000 for the lowest three bits (it has to be a multiple of 8) Those bits don't need to be stored. This allows a compressed oops to address 8 * 4 GB or 32 GB.

With a 16 byte object alignment the JVM can address 64 GB with 32-bit reference (however the padding overhead is higher and might not be worth it)

IFAQ: Why is it slower at around 28 - 32 GB

While the reference can be multiplied by 8, the heap doesn't start at the start of memory. It typically start around 4 GB after. This means that if you want the full 32 GB you have to add this offset, which has a slight overhead.

Heap sizes:

  • < 4 GB - zero extend address
  • 4 - 28 GB - multiply by 8 or << 3 note: x64 has an instruction for this to support double[] and long[]
  • 28 - 32 GB - multiple by 8 and add a register holding the offset. Slightly slower, but not usually a problem.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • ..thanks..but still 1 doubt what is header?? that to consuming 8-12 bytes?? – Prasanna Kumar H A Oct 12 '16 at 08:04
  • And It's *because 64-bit pointers use more space than 32-bit pointer.*...then what about compressed oops?? – Prasanna Kumar H A Oct 12 '16 at 08:05
  • 1
    @Sam the header is information which is at the start of the object. It includes the system hashCode of the object and the class pointer to say what type of object you have. Here are some examples of messing with the header https://github.com/peter-lawrey/Performance-Examples/blob/master/src/main/java/vanilla/java/unsafe/UnsafeIdentityDemo.java – Peter Lawrey Oct 12 '16 at 08:06
  • 1
    @Sam Compressed OOPS are 32-bit so don't use any more space than a 32-bit pointer. – Peter Lawrey Oct 12 '16 at 08:07
  • So object header and object reference is different...for object header 8-12 bytes memory and for reference 4/8 bytes memory(?)...is my understanding correct? – Prasanna Kumar H A Oct 12 '16 at 08:07
  • So 64 bit also by default acts as 32bit to consume less memory and to make perfomance fast..so there is no meaning to use 32-bit(until OS needs).? – Prasanna Kumar H A Oct 12 '16 at 08:09
  • 1
    @Sam you have a reference to the start of the header of an object. The size of the reference and the size of the header (or the object) are different. – Peter Lawrey Oct 12 '16 at 08:09
  • 1
    @Sam a 32-bit JVM is required on a 32-bit OS. There is a slight overhead in treating a 64-bit pointer as 32-bit esp when you have larger heaps like 28-64 GB but in general the more, larger registers helps – Peter Lawrey Oct 12 '16 at 08:11
  • @Sam for heaps up to around 28 GB, compressed OOPS doesn't have much overhead if at all. – Peter Lawrey Oct 12 '16 at 08:12
  • Confusing..Means till 28GB...compressed oops don't reduce(or dont come into picture) memory to 4 bytes(as 32bit uses 4bytes) – Prasanna Kumar H A Oct 12 '16 at 08:13
  • @Sam added my answer on why 32-bit references can address 32 GB of memory. – Peter Lawrey Oct 12 '16 at 08:16
  • still one thing comes into my mind..java don't have pointers only references but still why to mention *64-bit **pointers** use more space than 32-bit **pointer**.* Is it just a references? – Prasanna Kumar H A Oct 12 '16 at 08:22
  • 1
    @Sam At the virtual machine level, you have only references (and headers are an implementation detail which might not exist). Lifting the cover of what the JVM does, at the machine level, you only have pointers. The JVM can use tricks to translate references into pointers. – Peter Lawrey Oct 12 '16 at 08:25
  • please update the answer(convert comments to answer) with what is object header,and pointer and references...And what about primitive types are they require object header memory(i think not because they are not objects) but they also consumes 8 bytes because of padding even though int is just 2 byte(?). Thanks for your time. – Prasanna Kumar H A Oct 12 '16 at 08:38
  • @Sam an int is 32-bit and uses 4 bytes. For each primitive type there is a wrapper e.g. `Integer.BYTES` is `4` and `Double.BYTES` is `8`. About to give a talk in London, will review later. – Peter Lawrey Oct 12 '16 at 10:58
  • no problem:) whenever you are free at that time update it. – Prasanna Kumar H A Oct 13 '16 at 06:03
  • 1
    On today’s implementations, `String` has no `offset`+`count` fields anymore, so there’s an object header of 8 bytes (with compressed klass pointers), plus the `hashCode`+`value` fields, each 4 bytes (with compressed oops), hence a total size of 16 bytes, which does not require any padding. So we have 16 bytes per `String` instance, instead of the 32 told in the outdated graphics. Java 9 is going to use a `byte[]` to be able to use an 8 bit encoding where feasible (in practice, the majority of the strings), so the array size will be halved then too… – Holger Oct 14 '16 at 10:15