6 ms·
JEP draft: 64 bit object headers
- kichik 4y agoHow come a whole pointer is required for class pointer? Can't it be a small offset into a class table? Do many Java applications often require millions of classes?
- deleted 4y ago[deleted]
- exabrial 4y agoIt’s uncommon for millions in an average program unless you’ve got a special use case. Typical usage would be a couple thousand.
- remram 4y agoYou mean add a level of indirection to every class access? Also there is already compression for that pointer, making it 32 instead of 64 bits.
- saagarjha 4y agoTurning a pointer loading into an addition and then a pointer load is not that much overhead.
- remram 4y agoThat's how the current compression works, I guess I don't understand what you propose.
- saagarjha 4y agoI guess I am confused as to what the extra indirection is.
- chrisseaton 4y agoThe current compression scheme doesn’t involve reading any memory. Indirection through a lookup table would. But who cares? 99% of the time you just need class identity, not values from it.
- saagarjha 4y agoIf you need identity you can just compare indices instead of pointers, no?
- chrisseaton 4y agoWhat's what I just said? > 99% of the time you just need class identity, not values from it. So no need to deference the class pointer, or the table index.
- saagarjha 4y agoOk taking a step back I just realized you aren't 'remram :P But anyways my point was that in the old scheme if you wanted to know where the class was you would do some arithmetic on the compressed pointer to get a full pointer and now you can do a comparison, or if you want to read something do a dereference. Assuming the new layout is an array of classes you have a similar scheme where you have an index and you can compare those directly, and if you want data in the class you deref.
- chrisseaton 4y ago> in the old scheme if you wanted to know where the class was you would do some arithmetic on the compressed pointer to get a full pointer and now you can do a comparison Well also you didn't actually need to decompress the pointer first - compressed pointers are unique as well of course.
- saagarjha 4y ago
- dexwiz 4y agoDefinitely have seen a few enterprise stacks that reach this size. It’s not common, but it happens.
- paulirwin 4y agoI think I'm understanding correctly, but please correct me if I'm wrong... the class pointer is a pointer to the type (class) the object implements. So that would mean to exceed even an unsigned 32-bit pointer size, there would need to be more than 4 billion types in the program to exceed that, not objects. I can't imagine there are (m)any programs that hit that. And it definitely doesn't need to be a native 64-bit pointer. As pointed out in the proposal: "It may be possible to compress class pointers to less than 32 bits, at the expense of smaller addressable class space."
- maxfurman 4y agoAs other comments have mentioned, in the JVM every lambda creates a new type. So there are way, way more "types" created in Java than in a comparable program in another language.
- symaxian 4y agoIt does seem like some additional compression could be performed, if they wanted to keep it a pointer to avoid a table lookup I wonder if they could do something like ensure that all class definitions must exist in the certain contiguous chunk of memory that has a fixed size of 1-4 gigabytes which would let them shrink the pointer size down.
- layer8 4y agoApplication servers can run an arbitrary number of applications in parallel within the same JVM. Also, every lambda expression currently creates a new class. The interface-proxy mechanism also creates new classes at runtime. Classes are not expected to be a limited resource in JVM-land.
- exabrial 4y agoIs this part of the tck or jvm spec? I’m surprised such an implementation detail isn’t left up the vendors if so. But in any case this will be yet another awesome perf improvement bringing jvm closer to cpp speed.
- kjeetgill 4y agoThese enhancement proposals are often OpenJDK/hotspot specific. I do believe this left to vendors.
- pjmlp 4y agoIt is, other JVM implementations tend to also have their own extensions, for example J9 had AOT for years, long before OpenJDK started to play with it. It also had support for a special kind of value types (packed objects) with are seen as intrisics by its JIT. https://www.ibm.com/docs/en/sdk-java-technology/7.1?topic=poet-packed-objects-2 https://www.ibm.com/docs/en/sdk-java-technology/7.1?topic=po... I guess this was eventually dropped due to Valhala efforts.
- cowsandmilk 4y agoAppears the owner of this JEP just started as a Principal Engineer at Amazon. Curious if that's the source of some of the research in the motivation and what the savings would be just for AWS and Retail operations.
- dastbe 4y agoif you look at the associated github he’s been working on this for a year, probably longer.
- maxfurman 4y agoWhy are there so many unused bits in the header in the first place? 12% heap size reduction across the board sounds really nice but I feel like some historical context on the original design is missing.
- nayuki 4y agoProbably for alignment. You want the class pointer (whether 32- or 64-bit) to be easy to dereference with little or no preprocessing. It looks like the HotSpot JVM implementers decided to use 5 bits for lock and GC, and 31 bits for identity hash code (not sure why they didn't use 32). That adds up to 36 bits. You'd want to round this up to a multiple of 32 bits so that both the beginning of the object and the class pointer are aligned to 32 or 64 bits.
- origin_path 4y agoIf you look at the tricks needed to shrink it to 64 bits then you'll see why this is hard. Consider that this object header has to contain: - GC state bits. - The class metadata, i.e. pointer to a Class<> object. - Lock data, including possibly a pointer to a lock. - Possibly, an identity hash code. - Possibly, a GC forwarding pointer. Several of these are pointers! If you're on a 64 bit machine, just one* of them implemented naively if immediately 64 bits. In other words Java object headers provide a lot of stuff. To pack the whole thing down to just 64 bits requires a lot of clever implementation tricks, exploiting what exact combinations can appear, etc. Also note that this overhead isn't Java specific. In C++ you have malloc headers and then the C++ vtable pointer, in many cases. So just running "new SomeObj()" in C++ will immediately blow you past the heap overhead of Java after this JEP is merged, and if you need to add a lock and/or reference count to catch up with the Java feature set then you're at multiples of it.
- yakubin 4y ago1. C++ doesn’t use vtables for classes which do not have virtual methods (in practice most of them). And vtables are shared between objects of the same type. 2. “Malloc headers”? Malloc usually stores metadata in freed data. 3. C++ developers don’t call “new” for every single object, the way Java devs do. Idiomatic code defaults to allocating fixed-size objects inline (on stack or in a pre-existing bigger allocation), not in a separate allocation. 4. In C++ there are other ways to allocate dynamic memory than “new”. Arena allocators, directly calling mmap/VirtualAlloc/whatever makes sense in a given case.
- ardel95 4y agoI'm glad folks are starting to look at memory efficiency of Java programs - there's just so much improvements to be had there. For a fun exercise, take a look at memory footprint of storing a million entries in a HashMap<Integer, Integer>. Between "object header" overhead, "everything is a pointer to a heap allocated object" overhead, "GC needs enough headroom to remain effective" overhead, the actual memory consumption can be as much as 10x of a comparable C, C++, or Rust implementation.
- adgjlsfhk1 4y agoAnd this isn't even getting into special cases where it can be worse. There are lots of places where a better language can use an 8 bit integer, while if you want to use anything vaguely generic in Java, you have an 8 byte header (plus another 8 bytes for a pointer to it).
- exabrial 4y agoI would use the Apache Collections int map and move on though… I see the Map specifically oriented towards objects, not basic types
- winrid 4y agoThis is very true and the suck of Java today, but fyi you can improve this exact scenario by using a special IntMap etc, which there are many libraries for (LibGDX has some, and Caffeine has off-heap maps).
- JanecekPetr 4y agoNot Caffeine, that's the caching library. You probably meant one of FastUtil, Eclipse Collections, Koloboke, HPPC(-RT), they all provide primitive specializations of Maps and Collections. For off-heap maps, now that's more interesting! I know about Chronicle Map, MapDB, https://github.com/snazy/ohc https://github.com/snazy/ohc and https://github.com/RuedigerMoeller/fast-serialization/wiki/Off-Heap-Maps,-Persistent-Maps https://github.com/RuedigerMoeller/fast-serialization/wiki/O....
- 4y ago
- excsn 4y agoAh, Roman is the guy from RedHat who worked on Shenandoah GC. He has an excellent background on this and will be really glad to see when this is stable.
- akokanka 4y agoWonder what the original author intended unused space for.. Also wonder if moving both hash and bit set will change performance characteristics.
- thayne 4y ago> Wonder what the original author intended unused space for My guess is it is padding so that the class pointer is on an alignment boundary.
- deleted 4y ago[deleted]
- twic 4y ago> When the object is locked by a stack-lock or monitor, or if the object is forwarded by the GC (all of which is indicated by the lowest two bits of the header), the upper 62 bits of the first word of the header are interpreted as a pointer to the stack-lock, object monitor or the forwarded location of the object. The term "stack-lock" is new to me. What's that? Is that just when the lock record is stored on the stack? In which case, what is being locked by a monitor, because i would expect the lock record to always be on the stack. Is this about thin vs inflated lock states?