|
-
January 25th, 2010, 02:29 AM
#1
Garbage Collection
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
-
January 25th, 2010, 05:11 AM
#2
Re: Garbage Collection
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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 25th, 2010, 07:38 AM
#3
Re: Garbage Collection
 Originally Posted by hugo84
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
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.
-
January 25th, 2010, 07:39 AM
#4
-
January 25th, 2010, 08:00 AM
#5
Re: Garbage Collection
ok. thanks for all the inputs.. noted
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|