1

I have a program that needs to run at a roughly regular interval, forever and ever. I found the ScheduledExecutorService and noticed its scheduleAtFixedRate() method. There's a simple tutorial here.

My question is, if my program continues to run as it's meant to (about every 60 seconds until the end of time) is there anything that needs done to handle garbage collection if I use the above method? Do I need to shut down threads, etc? Should I run System.gc? I'm still getting used to Runnables.

At the moment I am using a while loop and thread.sleep(60000) to execute my task every 60 seconds. There's no garbage collection this way though, and after running for a couple days my program winds up using over 70% of the server's memory.

Thanks for any help you can give. I appreciate your time helping me with my newbie issue. This is essentially year 2 of my Java journey!

TheFunk
  • 981
  • 11
  • 39
  • Garbage collection runs automatically. If your process consumes more and more memory over time, then you have a resource leak. – Ralf Feb 29 '16 at 20:45
  • The java garbage collector uses a mark and sweep algorithm to reclaim memory (which means that when it runs, it must halt execution to garbage collect). The JVM calls the GC regularly throughout execution, but it doesn't specify when or how it will call it. It also means that on multithreaded programs, the GC may not clean the memory very well as the objects on the heap may be considered "still in use". [This](http://www.dynatrace.com/en/javabook/impact-of-garbage-collection-on-performance.html) might help. – callyalater Feb 29 '16 at 20:46
  • Read [this question](http://stackoverflow.com/questions/2085544/garbage-collection-and-threads) for a good explanation of Java GC in multithreaded contexts. – callyalater Feb 29 '16 at 20:49
  • @callyalater that actually helps a lot! Part of my program is multithreaded as that particular class communicates with a whole mess of remote devices simultaneously. I use Future to get the results from each of the remote devices. I'm going to guess this is possibly what's causing my woes. – TheFunk Feb 29 '16 at 20:51

1 Answers1

0

No action necessary. GC will automatically run when needed.

Andreas
  • 154,647
  • 11
  • 152
  • 247