I'm experiencing what I think is a memory leak, but I can't really explain it.
I have a Class, in which can start a task.
public class TestSingleton {
void start(){
var task = new ScheduledTask();
task.run();
}
static void setNextRequest(Date date){
var task = new ScheduledTask();
if(date != null){
var timer = new Timer();
timer.schedule(task, date);
}else{
task.run();
}
}
}
public class ScheduledTSLUpdate extends TimerTask {
@Override
public void run(){
String request = "http://localhost:2374";
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(request))
.headers(
"Authorization",
"Bearer " + Configuration.config.getProperty("JWTTOKEN"))
.build();
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
ResponseEntity responseEntity = new Gson().fromJson(response, ResponseEntity.class);
TestClass.setNextRequest(responseEntity.date);
}
}
Apparently, when I call start(), I get a memory leak, and eventually an OutOfMemoryError: Java Heap Space. In theory, the Timer and ScheduledTask Objects should be garbage collected after execution(?).
Please help me, I'm really at a loss here.
Edit:
Screenshot showing the Exception (I used GSON for deserialization)
Each time you run the task, it spawns a new task recursively, so obviously it will not stop until you run out of memory.
If you want to repeat a task, see Timer.scheduleAtFixedRate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.