CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    52

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection

    Quote Originally Posted by hugo84 View Post
    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.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection

    ---

  5. #5
    Join Date
    Aug 2009
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured