2

I'm new to working with java graphics and recently i've noticed that the project i'm working on has been slowing down (lagging, dropping frame rate). I think that the reason why is that instead of making an instance of an object and then drawing it repeatedly i've been making new instances each frame and drawing those. I want to make sure that that's the reason before I start reworking all my code.

thank you

  • It could be, but there's other things too that can cause problems: https://pavelfatin.com/low-latency-painting-in-awt-and-swing/ – markspace Jan 24 '18 at 22:09
  • http://www.oracle.com/technetwork/java/painting-140037.html – markspace Jan 24 '18 at 22:10
  • Object creation and GC has a notable overhead - [for example](https://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184), which uses a simple object pool to reuse objects rather then creating/destroying them (still needs work, but the basic concept is there) – MadProgrammer Jan 24 '18 at 22:19

2 Answers2

1

That is hard to tell without seeign the code but you should definetly create or update instances only when needed and draw them repeatedly.

Jan Husák
  • 331
  • 3
  • 12
0

I recommend that you profile your code. Use the profiling stats to decide whether your theory is correct or not.

We would still be guessing about the cause of your "lagging", even if we saw the source code. You should be spending time tuning or rewriting your code based purely on someone's guesses.

FWIW, the overheads of object creation are not as large as some people make out. But GC impacts on realtime behavior because even the best (low-pause) Java GCs have phases where they stop all application threads to perform essential tasks.

And this leads us to the fact that you may be able reduce "lag" by simply tuning the JVM's GC settings. (Assuming that you haven't already tried that .....) I would try GC tuning before doing a massive code rewrite.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216