Hacker News new | ask | show | jobs
by gok 328 days ago
Java is not memory-safe in the Rust sense.
1 comments

Can you elaborate on that?
A Java program can share mutable state between threads without synchronization, and it will compile and run. In Rust, such a program will not compile.
Yes, but even so you will never see e.g. an invalid pointer value as the result of a torn memory write. Basically, no matter what you do with threads in Java, it will not segfault.

TFA's point is that (safe) Rust is also like that, but achieves it by restricting all cases where a torn write could be observed through its type system instead of VM's memory model.

More specifically, Rust prevents data races.
No, rust forces you to use a mutex but nothing will prevent you from making the mutex too small and creating tearing in your own data structures by sequentially modifying things covered by mutexes so that in between acquisition of the locks you are violating invariants. The borrow checker certainly helps however, but not without cost that was finally minimized when the scoped threads api came along.

Java has a very specific memory model, so the behavior of variables across threads is quite well defined. Basic variables can tear however (a 64bit long on a 32bit architecture) without the volatile keyword and that is quite different than rust.

You didn’t describe any data races.

What Rust prevents is very specific.