Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:18:59+00:00 2026-05-20T07:18:59+00:00

I just encountered the following code (slightly simplified): /* periodically requests garbagecollect to improve

  • 0

I just encountered the following code (slightly simplified):

/* periodically requests garbagecollect to improve memory usage and 
   garbage collect performance under most JVMs */
static class GCThread implements Runnable {
    public void run() {
        while(true) {
            try {
              Thread.sleep(300000);
            } catch (InterruptedException e) {}
            System.gc();           
        }
    }
}

Thread gcThread = new Thread(new GCThread());
gcThread.setDaemon(true);
gcThread.start();

I respect the author of the code, but no longer has easy access to ask him to defend his assertion in the comment on top.

Is this true? It very much goes against my intuition that this little hack should improve anything. I would expect the JVM to be much better equipped to decide when to perform a collection.

The code is running in a web-application running inside a IBM WebSphere on Z/OS.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:19:00+00:00Added an answer on May 20, 2026 at 7:19 am

    It depends.

    1. The JVM can completely ignore System.gc() so this code could do absolutely nothing.

    2. Secondly, a GC has a cost impact. If your program wouldn’t otherwise have done a GC (say, it doesn’t generate much garbage, or it has a huge heap and never needs to GC) then this code will be added overhead.

    3. If the program would normally run with just minor GCs and this code causes a major GC, you will have a negative impact.

    All in all, this kind of optimisation makes absolutely no sense whatsoever unless you have concrete evidence that it provides benefit and you would need to re-evaluate that evidence every time the program materially changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just encountered this following code: public class TestFinally { public static void
I am debugging some code and have encountered the following SQL query (simplified version):
I just encountered the following behavior: for (var i = 0; i < 50;
I am just starting working with jquery and have encountered the following problem <div
Just going through the sample Scala code on Scala website, but encountered an annoying
I'm reading Sitepoints 2007 book Simply Javascript and I encountered some code I just
I was reading the Wikipedia article on SFINAE and encountered following code sample: struct
Today I encountered the following situation: (pseudo code) class MyClass { public void workOnArray(Object[]
I encountered the following problem, and I don't get any warnings or errors, just
I just encountered a weird error which saying that find is not a member

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.