Quartz Scheduler keeps creating new objects leading to OutOfMemoryError
I just implemented Quartz following a quick getting started guide:
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class Quartz {
public static void startScheduler() throws InterruptedException{
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// Grab the Scheduler instance from the Factory
scheduler.start(); // and start it off
JobDetail job = newJob(RepetitiveRun.class)
// define the job and tie it to RepetitiveRun class
.requestRecovery()// ask scheduler to re-execute this job
.withIdentity("job1", "group1")
.build();
Trigger trigger = newTrigger()
// Trigger the job to run now, and then repeat every x seconds
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(2)
// how often should the job repeat (once every x seconds)
.repeatForever())
.build();
scheduler.scheduleJob(job, trigger);
// Tell quartz to schedule the job using our trigger
try {
Thread.sleep(1200000000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // how many seconds the Main should run
scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
The RepetitiveRun class, which implements Job, executes requests and writes them to files.
The problem is that the heap grows continuously and after a while java.lang.OutOfMemoryError is thrown.
Using VisualVM I can see that Live Bytes, Live Objects and the generations associated with the RepetitiveRun class are constantly growing, which suggests that this is the cause of the memory leak.
Is it possible to make Quartz delete / deallocate all objects created during a job that has already been completed?
source to share
This is usually a job for the garbage collector (clean up after shutdown).
When the job has finished and there are no more references to the job, the garbage collector should clean up.
I suspect that you either missed your work outside of it (for example, pass this
to any other object that stores a link to your assignment).
Or you are not clearing resources that should be cleared by you (such as database connections or files).
This leads to a memory leak that you have to track with a profiler and clean up what is needed (either not leaking your object outward, or closing all resources you open / create).
source to share