3 ms·
Maintaining a garbage collector requires overhead, hence why C++ does not implement one as part of the language. From my experience at University, the JVM uses
by daemin 15y ago
Maintaining a garbage collector requires overhead, hence why C++ does not implement one as part of the language.
From my experience at University, the JVM uses an indirect method of accessing and storing objects. So each reference to an object is effectively a handle, hence as big as a pointer. It points to a table that contains the actual pointer to the object in memory (or something to that effect), so that the garbage collector can move the object in memory.
Therefore an object that just has a single integer will require 2 pointers and an integers worth of memory (at least). Any less than that and it would be garbage collected since there are no live references in the code to the object.
Garbage collection isn't free.
- carey 15y agoWhile that’s one way to do it, I believe that current JVMs use an index directly to the target object in the Java heap. Since the garbage collector is following all the references anyway, it can eventually update all the old references to point directly to an object after moving it. In 64-bit VMs without a very large maximum heap size, the JVM usually uses "compressed" pointers, too, so 32-bit values can, in practice, address 32GB of data. Intel CPUs have addressing modes that work quite well with this. See: http://wikis.sun.com/display/HotSpotInternals/CompressedOops http://wikis.sun.com/display/HotSpotInternals/CompressedOops Garbage collection isn’t free, but it’s getting pretty good.