nearly month ago testing javafx graphics api, worried performance. performance problem had pass paint
object colorizing shapes.
issue: it's impossible change settings of existing paint
i need update paint
, example, color
, there no methods color#setred
, color#setgreen
, color#setblue
, color#setopacity
. there no fields, (javafx.scene.paint.color).
performance
the unique way can try change settings of paint
construct paint
instead, involve on running garbage collector. correct?
the compiler should optimize these objects automatically depending on how they're entirely used. oracle said nothing on compiler, @ least me, far.
if garbage collection good, fault isn't mine. didn't mention garbage collection being helpful.
so solution re-construct paint
? guys know of javafx alternatives handle want?
the issue concerned doesn't exist. since paint
immutable class (which design choice number of reasons), correct way update color of in javafx create new paint
instance , set property needed. previously-used instances no longer have references them cleaned - efficiently - garbage collector @ point in future.
here simple demo creates new paint
instances every time scene rendered (also new background
, backgroundfill
instances). prints warnings if frame rendering takes more 25ms (an arbitrary threshold). on system, warns first couple of frames, , warns when window hidden , reshown.
import javafx.animation.animationtimer; import javafx.application.application; import javafx.geometry.insets; import javafx.scene.scene; import javafx.scene.layout.background; import javafx.scene.layout.backgroundfill; import javafx.scene.layout.cornerradii; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.stage.stage; public class updatecolorcontinuously extends application { @override public void start(stage primarystage) { pane root = new pane(); root.setminsize(600, 600); new animationtimer() { private long start = -1 ; private long lastupdate ; @override public void handle(long now) { if (start < 0) { start = ; lastupdate = ; } long elapsed = - start ; double elapsedseconds = elapsed / 1_000_000_000.0 ; color newcolor = color.hsb(elapsedseconds * 5, 1.0, 1.0); backgroundfill fill = new backgroundfill(newcolor, cornerradii.empty, insets.empty); background bg = new background(fill); root.setbackground(bg); if (now - lastupdate > 25_000_000) { system.err.println("warning: frame rendering took "+ (now-lastupdate)/1_000_000 + " ms"); } lastupdate = ; } }.start(); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
it's possible haven't run long enough gc need run; conservative estimate of amount of memory paint
instance needs 50 bytes, need create of order of 10^7
such instances use 500mb (a reasonably small amount of ram on modern desktop system, equivalent 1 gc sweep). so, ballpark figures, generate 1 gc sweep in 2 days.
Comments
Post a Comment