News for hackers. Stuff that matters.
Re:Java 21 makes me like Java again by gabereiser on Saturday September 16, 2023 @06:11PM (#37539285)
>” None of the memory problems of C++”

I’m sorry, I’m calling BS here. You can still leak memory in Java.

Re:Java 21 makes me like Java again by vlovich123 on Saturday September 16, 2023 @06:15PM (#37539320)
You can leak memory in any language. That’s an impossible bar. The problems op is referring to is obviously about memory safety.

Java obviously isn’t thread safe like Rust is, but it’s typically safer than C++ on that front too.

Re:Java 21 makes me like Java again by gabereiser on Saturday September 16, 2023 @06:33PM (#37539467)
Rust isn’t thread safe either. It’s borrow checker will attempt to correct you but you can still run Rust unsafely.
Re:Java 21 makes me like Java again by laurencerowe on Saturday September 16, 2023 @10:45PM (#37540985)
As I understand it Rust's unsafe is designed so you can encapsulate unsafety behind a safe interface.

It's up to the programmer to verify the safety of code using unsafe. But it they do so correctly then you can rely on the borrow checker to verify that everything else is safe.

This means that when you run into a thread safety bug in Rust code you should only have to look at the unsafe blocks to find the culprit.

Re:Java 21 makes me like Java again by kaba0 on Sunday September 17, 2023 @03:03AM (#37542283)
With that said, I think it is important to mention that if you go off the safe path in rust and you do hit such a bug, your current execution is no longer trustable, it could have corrupted the heap, introduce UB, and die with a segfault, etc.

In java, race conditions can enter illegal application state, but their scope is much more limited. NPEs are 100% safely handled, etc. you can only get off the safe road with using Unsafe unsafe, and manipulating the JVM’s inner state which is not even allowed by default. Depending on application, this difference may matter a lot!

Re:Java 21 makes me like Java again by quickthrower2 on Saturday September 16, 2023 @10:52PM (#37541038)
There are two kinds of leaks IMO. The one where your algorithm allocates gigabytes needlessly (and cannot be avoided by language design) and more insidious stuff like having to unsubscribe from an event handler for your object to be dereferenced.
Re:Java 21 makes me like Java again by c048 on Saturday September 16, 2023 @06:34PM (#37539472)
Shitty code is shit in any language. What's your point?
Re:Java 21 makes me like Java again by ndr on Saturday September 16, 2023 @06:18PM (#37539347)
Isn't that true in any language that lets you allocate memory?
Re:Java 21 makes me like Java again by kaba0 on Saturday September 16, 2023 @07:05PM (#37539703)
var list = [];

while (true) { list.add(1); }

Here you are, which language won't leak memory here?

Also, which language will let you connect to a prod instance without a performance hit to get some stats on the heap and its allocated objects? Hell, you can even list every instance of a type as I've recently learned.

Re:Java 21 makes me like Java again by packetlost on Sunday September 17, 2023 @12:29AM (#37541530)
>var list = [];

> while (true) { list.add(1); }

idk, I don't think endlessly growing memory usage is really what a leak is, a leak is really when you have no way of accessing the allocated memory to free it (ex. dropping the last remaining pointer to a `alloc`'d block in C) or the opposite in garbage collected languages: accidentally holding onto a strong reference to objects that should be freed.

Re:Java 21 makes me like Java again by kaba0 on Sunday September 17, 2023 @03:04AM (#37542289)
> the opposite in garbage collected languages: accidentally holding onto a strong reference to objects that should be freed

So basically what I gave an easy example of. Sure, it won’t look like this in practice, you probably accidentally keep adding to a list, or a cache, but basically this is what happens. The former kind of leak can’t happen with tracing GCs.

Re:Java 21 makes me like Java again by EdwardDiego on Saturday September 16, 2023 @07:07PM (#37539726)
You can. But you have to try rather hard.