News for hackers. Stuff that matters.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by tjansen on Thursday July 18, 2019 @11:17AM (#20470698)
Closing files manually is manual resource management, not memory management. C++ uses its memory management for resource management, but Java doesn't. Java improved resource management with "try with resources", but generally resource management really isn't an issue for most Java developers. In modern frameworks like Spring you rarely have to manage resources yourself. You let the framework manage them for you, and the framework will provide them to you via injection.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by ErotemeObelus on Thursday July 18, 2019 @11:56AM (#20471121)
I want a language where the garbage collector not only frees inaccessible dynamically allocated blocks, but also frees all resources when they're inaccessible. Is it possible to design such a generalized GC?
Re:Ask HN: Why do langs with alleged “automatic memory manag... by tjansen on Friday July 19, 2019 @03:48AM (#20476893)
Java's GC does it. When a reference becomes unreachable, it will automatically free the resource. That's not the issue. The issue is that GCs are not predictable. All current implementations either have issues with circular references, or they run infrequently and may even need several runs to clean up complex structures. So it may take a while until an unused resource becomes available again, and you don't know when.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by jarjarbinks455 on Thursday July 18, 2019 @01:58PM (#20472341)
In c# you can implement IDisposable. The garbage collector will call a dispose method allowing you piggyback on the garbage collector for just about anything.

But it's rarely a good idea to piggyback. Most resources are much more limited or time sensitive than memory. So you should free them immediately.

Even when your resource -is- memory it sometimes is a very very very bad idea to free via garbage collection.

Re:Ask HN: Why do langs with alleged “automatic memory manag... by ErotemeObelus on Thursday July 18, 2019 @02:13PM (#20472490)
What is even the point of garbage collection? By the time you manage all the resources of a 100k line application you're going to have to do as much work as if you wrote it in C.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by jarjarbinks455 on Thursday July 18, 2019 @02:23PM (#20472601)
Well the point of gc is to allow the algorithm to focus on the problem domain. and not worry about mechanical implementation details of a computer. Everything has a trade off. An obvious win still must pay a cost elsewhere. In the case of GC it's simply not a good trade off for many resources. Sometimes it cannot be used even if you were willing to accept a trade.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by ErotemeObelus on Thursday July 18, 2019 @02:33PM (#20472698)
Managing resources by hand is managing the mechanical implementation details of a computer. So in order to have garbage collection that manages more than just a sliver of memory so small that it counts as a rounding error by the same you get to the size of real world projects, you need a custom operating system and possibly even custom hardware too.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by AnimalMuppet on Thursday July 18, 2019 @06:12PM (#20474432)
Your statement is false for many real world projects. (For some, you are correct. Those projects should not be written in a garbage-collected language, or should be written to run on a custom OS and custom hardware.)
Re:Ask HN: Why do langs with alleged “automatic memory manag... by erik_seaberg on Saturday July 20, 2019 @01:07AM (#20484284)
When GC'd objects represent resources like open connections and files, use-after-free safely and reliably raises an exception to handle. Without GC, use-after-free on pretty much any object is so catastrophic that even correct code starts doing unpredictable things.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by Someone on Thursday July 18, 2019 @08:30PM (#20475277)
GC can do that, for a loose definition of when they’re inaccessible (it will do it when, but not as soon as)

Reference counting almost gives you as soon as, but it has trouble handling circular references. That is fixable, but not, AFAIK, in a way that’s both reasonably performance, frees resources as soon as they become unreachable, and isn’t too restricting.

Re:Ask HN: Why do langs with alleged “automatic memory manag... by bjourne on Saturday July 20, 2019 @12:54AM (#20484247)
What fix are you referring to? The ones I know of are algorithms for detecting when the object graph becomes disconnected. Unfortunately that turns out to be very hard in practice and you often have to scan a large portion of the heap. So it doesn't offer any major advantage over reference counting + mark & sweep gc. Which is kind of the worst of both worlds. :/ Ref counting overhead + potentially long gc pause times.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by Someone on Saturday July 20, 2019 @01:27PM (#20487092)
Ones that are either not reasonably performant, don’t free resources as soon as they become unreachable, or are too restricting.

The fix you refer to is one, but it falls under ‘not reasonably performant’.

One could also make it illegal to have cycles (easy way to do that: make it illegal to update any references). For many, many, that will fall under ‘too restricting’, though.

Alternatively, allow updates to references, but give every allocated object a ‘bit’ that indicates whether, from it, a reference can be reached that contains such an updated reference that points to an object created later (maintaining that property is doable, but may not be ‘reasonably performant’)

You can use that later: when the reference count of an object goes to zero, and the object has a reference to an object O with that bit set, check whether the graph reachable from O can be reached from elsewhere. Again, that’s doable, but likely not performant.

It somewhat depends on how common circular references are in real world code, though. I’m not aware of any papers researching that.

Re:Ask HN: Why do langs with alleged “automatic memory manag... by AnimalMuppet on Thursday July 18, 2019 @11:37AM (#20470919)
Your way of saying it isn't wrong, but I think I would say instead that C++ uses destructors both for memory management and management of other resources.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by brianush1 on Thursday July 18, 2019 @04:17PM (#20473551)
If we're being pedantic here, destructors don't manage memory at all, they only free resources. Memory is freed by the caller of the destructor, either automatically if the memory was allocated on the stack, or manually by the delete operator if the memory was allocated on the heap.
Re:Ask HN: Why do langs with alleged “automatic memory manag... by Someone on Thursday July 18, 2019 @08:22PM (#20475233)
Memory is a resource, too, and C++-style destructors do manage it, sometimes.

Destructors do not free the memory of the object being destroyed, but they can free memory that its fields point to.