Hi, I am working on a scientific visualization project for my degree and it requires us to draw many circles to the screen representing trees. This we did via the following code which runs as a thread and updates its PGraphic called graphics each time we filter or change zoom. Currently, if I try to re-render by clicking on the screen and moving around it works perfectly but if I do it too quickly and the threads get interrupted (which we allow) ram usage spikes up and we never get it back.
I have tried all sorts of fixes including using removeCache and image.dispose() as well as system.gc().
The only thing that fixes it is called g.removeCache on the specific graphics and System.gc() every frame (done in draw). This works perfectly but is obviously super inefficient and we drop frames madly.
Other weird behaviour is if I call only g.removeCache but do it every frame the memory spikes up and down to and from the same places (eg 700mb to 1.2gb in a few seconds and then back down to 700mb). Any help would be appreciated
while (true) {
try {
if (current) {
sleep(10);
//yield();
continue;
}
int currentBuildCount = buildCount.get();
// drawing loop
graphics.beginDraw();
//graphics.scale(1.0 / 2);
graphics.clear();
graphics.noStroke();
graphics.blendMode(ADD);
graphics.ellipseMode(CENTER);
for (PlantTree.Node node : plants.filteredPlantsInRect(newPos, zoom.layerPlantRadius(layer).mult(1.1))) {
color speciesColor = species.colors.get(node.plant.speciesId);
graphics.fill(speciesColor, 200);
if (buildCount.get() != currentBuildCount) {
//System.out.println("Thread restarted!");
g.removeCache(graphics);
break;
}
// draw current plant
PVector scaledPlantPos = zoom.plantSpaceToLayerSpace(node.plant.pos, newPos, layer);
graphics.circle(scaledPlantPos.x, scaledPlantPos.y, max(node.plant.canopyRadius * 2 * zoom.layerScale[layer], 1));
}
graphics.endDraw();
if (buildCount.get() != currentBuildCount) {
//System.out.println("Thread restarted!");
g.removeCache(graphics);
continue;
}
// set pos after image is finished
transferPixels(graphics);
g.removeCache(graphics);
graphics.clear();
this.pos = this.newPos.copy();
current = true;
}
}
}