5 ms·
You can write Java code to skip GC and manage memory by yourself. Like C++ http://www.mkyong.com/java/java-write-directly-to-memory/ http://www.mkyong.com/java/
by stevesun21 10y ago
You can write Java code to skip GC and manage memory by yourself. Like C++
http://www.mkyong.com/java/java-write-directly-to-memory/ http://www.mkyong.com/java/java-write-directly-to-memory/
- fauigerzigerk 10y agoIt's nothing like C++. In C++ you get automatically managed memory on the stack. You get RAII and smart pointers to help with heap allocations/deallocations. Most importantly, you get to _use_ the system's malloc implementation whilst you have to _implement_ your own malloc with the Java off-heap solution you suggest.
- stevesun21 10y agoI'm not a C++ expert, RAII sounds to me is like a synchronous GC in C++ -- resource auto alloc and dealloc by it's lifetime (https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initia...), correct me if I am wrong. Java, in another way, it has an asynchronous GC -- use different generations, and not release object right away just because out of scope. Anyway, I am saying 'like C++', I meant the possible way C++ can manually manage the memory directly, learnt from what you mentioned, I should say: 'like C'. Thanks.
- catnaroek 10y agoRAII is an automatic memory (more generally, resource) management scheme, but it isn't garbage collection. A garbage collector is a runtime component that reclaims unused storage. What RAII does is, at compile time, insert the appropriate resource-freeing calls at the right places.
- fauigerzigerk 10y agoThe problem with Java off-heap designs compared to C is that many Java language features become useless (e.g. instance variables, polymorphism, ...) and you lose the standard library as well as almost every single third party library out there. In my view, sun.misc.Unsafe is extremely unproductive to work with. But I will say that it may be the right solution for adding a small very spcialized feature to a larger Java application.