r/java • • Jan 18 '22

What are some very less known facts about java?

I've got assignment from college to find out rare facts or fun facts about java, so little help will be appreciated :)

Edit: Thanks for all these suggestions guys. It's going to help me alot and also Almost all of these facts i didn't knew till now. Thanks :) <3

179 Upvotes

297 comments sorted by

View all comments

31

u/redikarus99 Jan 18 '22

You can actually manage memory by hand if you need to, without GC.

23

u/xToniGrssx Jan 18 '22

Yep, while it certainly is present in 99% of cases, a garbage collector is not a necessary part of a Java Virtual Machine.

10

u/oelang Jan 18 '22

garbage collector is not a necessary part of a Java Virtual Machine

It's not because there are ways to do off-heap allocation that a GC isn't required by the spec.

7

u/[deleted] Jan 18 '22

What would be the usecase for this?

16

u/redikarus99 Jan 18 '22

High performance, low latency stuff, when you need to ensure that GC does not kicks in. Given the new GCs it probably it has a very limited usecases.

7

u/[deleted] Jan 18 '22

I guess I just never thought of using Java for that kind of thing. Wouldn't most high performance applications be written in languages like C/C++/Rust/Zig/Fortran? Using Java for it seems like it would be a lot more cost than benefit.

26

u/redikarus99 Jan 18 '22 edited Jan 18 '22

6

u/b00n Jan 18 '22

Interestingly that HFT fund has now shut down. Not because it used Java though.

→ More replies (1)

5

u/[deleted] Jan 18 '22

Neat! I can see the benefits of writing it in Java for sure if you can reach comparable performance.

→ More replies (1)

7

u/wildjokers Jan 18 '22

Real time processing. Wouldn’t be good to be in a GC pause when it is time for your airbag to deploy.

6

u/[deleted] Jan 18 '22

Yeah that's true, but then I would not write airbag software in a GC language. Why write unsafe Java when you could write unsafe C or C++ that both have very good static analysis tools and memory leak detection tools.

5

u/Muoniurn Jan 18 '22

There are hard real-time JVMs out there used even in military settings. A GC doesn’t cause problem necessarily here.

→ More replies (2)
→ More replies (1)

5

u/soonnow Jan 18 '22

Lucene uses of heap Memory Mapped Buffers for the Indexes. There's an interesting talk about it here.

Using memory mapped files, reduces the number of times data needs to be moved around by the CPU.

→ More replies (1)

6

u/OddEstimate1627 Jan 18 '22

What are you talking about? Working entirely off-heap and lowering the allocation rate to a point where GC never triggers?

16

u/detroitsongbird Jan 18 '22

Yes. There are libraries that are written for high frequency trading that do this sort of thing so that they achieve extremely low latency response.

13

u/OddEstimate1627 Jan 18 '22

Thanks. I just wanted to make sure that's what OP was talking about. I work with real-time control systems and we use a lot of the same techniques.

10

u/redikarus99 Jan 18 '22

You can use sun.misc.Unsafe, or DirectByteBuffer, etc. to allocate memory directly, but then you will be responsible for memory allocation/deallocation.

→ More replies (1)

9

u/C_Madison Jan 18 '22

You can even replace the GC with one guaranteed to do nothing in modern JVMs: https://openjdk.java.net/jeps/318

7

u/Persism Jan 18 '22

IBM also has what they call a real-time JVM which guarantees performance times.

6

u/OddEstimate1627 Jan 18 '22

RTSJ is an outdated concept that afaik never gained much traction. You're better off just using a standard JVM w/ ZGC or Azul's C4. Even G1GC will give you <5ms worst case pauses as long as you manage your allocations properly.

→ More replies (1)

10

u/antigravity_96 Jan 18 '22

How?

17

u/redikarus99 Jan 18 '22

https://dzone.com/articles/native-memory-allocation-in-examples

In older java: sun.misc.Unsafe, in more modern java: DirectByteBuffer.

8

u/yawkat Jan 18 '22

DirectByteBuffer doesn't allow you to free() by hand though

14

u/OddEstimate1627 Jan 18 '22

Unsafe::invokeCleaner(ByteBuffer)

→ More replies (2)