The overhead for smart pointer is negligible 99% of the programs out there (especially user interface). While an additional garbage collection thread is a massive overhead that might even stop the relevant proccessing. |
If you were right, how come, in my evolutionary optimisation program, the GC thread accounts for less than 0.1% of CPU? GC is a problem if you don't understand how it works. I know one C++ programmer that killed JVM's GC by putting a finalize method in every single class he defined (and if you need a finalizer in Java, you're certainly doing something wrong, with some rare exceptions).
BTW, the additional GC thread is a huge advantage - on a multiprocessor machine, it can sit on a spare core(s) and make the total GC overhead exactly zero. Add to that zero cost deallocation and a blazingly fast allocation (about 10x faster than malloc) and clearly GC is the performance winner.
The only problem that GCed applications might encounter are pauses, but this is due to poor Oracle's GC implementation, not due to the GC concept itself. Pausless efficient GCs do exist. And smart pointers can block the thread for a long time, too.
In Java it stays in memory long after it's used. |
So what? In C++, too. That you called "delete" on something, doesn't really mean the memory is returned to the OS, because of fragmentation (only full clean pages may be returned to the OS).
The GC can move objects around in memory because the program is not aware of their addresses, in the same way that the OS can swap pages in and out of physical memory because processes only know their virtual addresses.
Remember, kids: All* problems in computer science can be solved by adding another level of indirection. |
Wrong. There is no another layer of indirection in the JVM memory model. The application uses "real" pointers, not some virtual handles. The difference is that the JVM knows where all those pointers are and fixes them when objects are moved.
But is it really better than system? |
In C++, new, delete and malloc, free are not system calls. Actually they have very little to do with the system. The system doesn't know anything about your C++ "objects". What does know is the userland C++ runtime, which contains the memory allocator, just as the JVM contains the GC. The architecture is very similar, the implementation is very different. Java's GC does not use the new/delete, malloc/free calls to manage memory. It replaces them.