How do you determine when to use System.gc? before the last line of the program?seems pointless? how about in the middle of the program? Please explain..thanks
Printable View
How do you determine when to use System.gc? before the last line of the program?seems pointless? how about in the middle of the program? Please explain..thanks
In general, you don't need to. The system calls it for you when necessary. However, you can request a garbage collection at a suitable time in your application. A suitable time could be after some memory-intensive processing and before some time-dependent interaction where a garbage collection pause might be perceived as inconvenient (e.g. a user interaction). It may find some use in real-time processing, but there are alternative garbage collection schemes that can be used in specialised situations.
I would suggest treating it as an optimisation and not using it unless you can identify a problem for which garbage collection is the solution. Usually good application design and the use of a profiler can avoid any need for such calls.
See Tuning Garbage Collection.
It is easier to measure something than to understand what you have measured...
Anon.
The best approach is to never call System.gc. Why? Because the GC runs all the time and it knows wastly more then you do about the runtime characteristics of your program. It certainly doesn't need your "help". In fact calling System.gc may make things worse. Why? Because you may induce GC runs that are completely unnecessary.
Sun has spent millions on the GC. Don't spoil that investment by messing around with it. :)
---
ok. thanks for all the inputs.. noted