surlac surlacovich wrote:
Randall Twede wrote:it is very good at doing it's job. btw, calling gc() does not ensure that it will run.
I agree that its doing good job, but I think it would be better to also have retain/release/autorelease mechanism which will allow to count references by yourself and release(deallocate) object right away, because as everybody knows GC cleans up memory with delay.
The garbage collector in current Oracle JVM versions is much more sophisticated than you think. Garbage collection is a huge topic for which a lot of research has been done. So, it's hard to draw any conclusions about how efficient it is without being an expert. (I'm certainly not an expert with regard to GC).
Since Java 6 update 23, the Oracle JVM also includes
escape analysis - a technique that checks if short-lived objects can be allocated on the stack instead of on the heap, which makes cleaning them up very cheap (in fact, it's automatic, when the method returns and the call stack unwinds). That sounds more or less like what you're proposing.
Fortunately escape analysis is also automatic, it would be really cumbersome if we as programmers would have to spend time thinking about low-level optimizations all the time.