I want to know, why java (or gc-based languages like C#, GO) haven't any tools for direct memory management tools in addition to gc.
I am familiar with JLS and JVM spec, but I didn't find out any explanation of that.
It can boost performance (reducing amount of work for gc) and can decrease amount of used memory when developer can directly delete objects that he surely know aren't needed anymore.
I'm not familiar with python memory specification, but I know, that there are gc() method and keyword del for memory management.
P.S. I was googling for an hour, but I only find answers describing that I shouldn't care about deleting objects in memory management languages.
Memory Management in Java refers to allocating and deallocating memory to java objects which reside in areas called Stack and Heap. Java has an automatic memory deallocation system known as Garbage Collector.
In Java, the programmer allocates memory by creating a new object. There is no way to de-allocate that memory. Periodically the Garbage Collector sweeps through the memory allocated to the program, and determines which objects it can safely destroy, therefore releasing the memory.
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
The direct buffer memory is the OS' native memory, which is used by the JVM process, not in the JVM heap. It is used by Java NIO to quickly write data to network or disk; no need to copy between JVM heap and native memory.
Java has always been security focussed, though never as important as ease of use. Non-garbage collected memory management is difficult to guarantee memory safety use, though see Rust for a good stab at it.
Good JVM implementations can stack-allocate "heap" objects in some time-critical circumstances. GC for short lived objects is incredibly fast anyway.
You can allocate memory outside of GC using the standard Java Class Library - java.nio.ByteBuffer.allocateDirect
. There isn't as yet, IIRC, a way to explicitly deallocate. That is does indeed require the GC to trace down all the references. The API will work well if you need fixed arrays of primitives that will last as long the process.
"It can boost performance"
how? explicitly free up memory takes time by itself. how is that better than letting the GC do its magic?
"decrease amount of used memory"
why should you care about the used memory? most modern applications employ "high water mark" strategy where they retain unused memory for future use.
"he surely know aren't needed anymore"
the cases where you have this certainty are becoming rare. any web application, even "hello world", deployed on modern servlet container/web server/web framework is too complex to let the developer decide on memory management.
It has more or less. It calls Unsafe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With