by bobsgame · 9y ago · 0 comments
How impossible would it be to add a delete keyword to the JVM, and why?
A "delete now" operation imposes requirements on implementations that are not strictly required.
Any advantages gained from destructors can be realized via explicit cleanup methods and (as a hedge against mis-use) state checking for initialized and destroyed objects. A delete operation also adds risk that an object that has been deleted may be referenced again (although with esoteric stuff like PhantomReferences you can almost do it).
http://www.docjar.com/docs/api/sun/misc/Unsafe.html#freeMemo...
If you want manual memory allocation then you should instead look at sun.misc.Unsafe.
http://www.docjar.com/html/api/sun/misc/Unsafe.java.html
public native long allocateMemory(long bytes);
public native void freeMemory(long address);
Of course you still have to calculate the field offsets and all that stuff manually because it's not a part of the java language but you only asked about the JVM.Manual memory management is a good choice if you have both worst-case latency concerns as well as very restricted RAM and/or severe energy constraints (basically, the kind of applications that Rust was designed to handle).
That’s horrible to use for game development.
If those 11 threads would share the same heap and 1 thread still doesn't use the gc because it's using manual memory management then you'd still suffer from stop the world pauses.
What you want is isolated heaps like erlang does, not manual memory management. I'm still wondering why we have no other programming languages with multiple heaps. You could probably achieve something similar with D and use of memory mapped files to efficiently share memory without copying bewteen processes. Alternatively you could call into C to spawn OS threads that don't suffer from stop the world pauses. But those two options are a hack. You're not supposed to use D like that.
I chose D as an example because it's a programming language with both a garbage collector and manual memory management that still suffers from stop the world pauses.
(Keep in mind that Java is a memory-safe language.)