News for hackers. Stuff that matters.
Re:Oracle Tunes Java's Internal String Representation by MaulingMonkey on Wednesday December 25, 2013 @12:10PM (#6963426)
> If there was an additional type of weak-like reference, that had a callback with an object before it was garbage collected, this conundrum would be simple.

You've basically described weak references to objects with finalizers. Unfortunately, I'd wager this is even more expensive, not less.

Re:Oracle Tunes Java's Internal String Representation by twic on Wednesday December 25, 2013 @12:41PM (#6963562)
No, TheLoneWolfling's idea can't be implemented with weak references with finalizers. Firstly, finalizers only run after weak references are cleared (i think - please correct me if i'm wrong!). Secondly, he needs GC to trigger code that mutates the no-longer-referring object; the finalizer runs in the no-longer-referred-to object.

He's right that a new VM-level hook would be required to implement his idea. The benefits don't clearly justify making a change of that magnitude.

Re:Oracle Tunes Java's Internal String Representation by MaulingMonkey on Wednesday December 25, 2013 @02:16PM (#6963897)
Depends. I'm imagining e.g. your large parent string invasively giving it's sub-slices copies on finalization through it's own list, in which case the weak references clearing isn't much of an issue. Not easy to make fast and thread safe however.

In C#, WeakReference can take a boolean parameter during construction to specify if it tracks post-finalize or not (defaulting to the same behavior as Java which google tells me is as you say: not), and you can re-register your finalizer, but even so this kind of thing isn't what the GC is primarily designed for and it will probably show. I'm not sure if you can re-register finalizers in Java.

Re:Oracle Tunes Java's Internal String Representation by ajanuary on Wednesday December 25, 2013 @06:57PM (#6964754)
Finalisers run before weak references are cleared. You do, however, have the GC cost of having a finalized.