Rust doesn't provide memory safety in the GC sense — it can and will leak memory in ways Java wouldn't. Rather, it provides safety from all data races, which is something that conventional GCs don't give you.
Java doesn’t prevent memory leaks either. Java will happily allocate up until an OOM. If an object is live, it will not be garbage collected.
In fact Java will leak memory over FFI boundaries as well. Rust is memory safe, but like many languages it is not leak safe. It is possible in Rust to tell it to leak memory, but that’s for special cases.
To state this another way, memory leaks are different from memory safety.
"Memory Safety" doesn't refer to leakage, it refers to read-after-free, buffer overflows, and other memory-access related bugs. Garbage collection runtimes keep track of memory access to avoid these issues, rust does it through lifetimes/ownership.
How does java prevent memory leaks which rust couldn't ? Do you have examples for that? I was not aware that Java does it and am very interested in that.
One of the advantages of a tracing garbage collector is that it can recognize cycles. This means that if you have a few objects which point to each other, but the overall object graph is dead, a tracing GC should be able to collect these objects.
If you use reference counting in Rust, it will not be able to detect cycles. That said, it's not super easy to get a cycle accidentally.
True, but it's safe with memory in ways that non-GC'd languages usually aren't (buffer overflows, use after free, etc.) so it's worth mentioning the GC bit.