Java applet freezes for 5 seconds or more... We recently finished a java applet card game (Fairway Solitaire) that tends to freeze at completely random times once or twice every hour. These freezes will last for five to ten seconds and they basically cause everything on screen to stop moving or animating. To make matters worse, players will often click on the draw pile, and then, when nothing happens, click it again. So when the game comes back both mouse clicks are processed and players end up drawing two cards, thus wasting one. The programmer said there is nothing in the code that should cause the game to pause like that and we're wondering if it's automatic garbage collection. But would GC cause delays of five to ten seconds?? Is there anything we can do to improve this situation?
That's pretty weird. I've never heard of it happening before. I'm 99% sure it's something in your code that's causing the problem though, and not a Java bug or some such. It does sound like your problem could be related to garbage collection. If it was, I would guess that you keep allocating more and more memory without any of your variables going out of scope and getting garbage collected. Then, when memory gets low, Java "forces" a deletion of certain variables. You could check if something like this is happening by just doing a ctrl+alt+delete and checking the amount of memory Java is using (assuming windows). (That's just a guess - I'm not really sure how Java works under the hood). Another random thing that could be going wrong is if your using fancy thread stuff... for example sychronized doesn't work with applets (or is it Java 1.4 it doesn't work with? anyway, it doesn't work for me). Just a thought.
Try forcing a garbage collect every so often, or at points where a potential drop in performance won't be noticed. I notice a pause in my game every time mail arrives....
Thanks for the replies! Can garbage collection take five to ten seconds?? When I heard about this automatic feature in Java I assumed that the time would be measured in milliseconds. (Not thousands of milliseconds.) Any other thoughts? The pauses seem to come at completely random times and we've seen this behavior in other games, as well.
Sounds quite weird to me. Do you have any specifics of hardware setups where this happen, including used browser & Java version?
It has been reported by a large number of our users, so I suspect a diverse range of hardware configurations and java versions (we require 1.5 or better).
Some programmers like to point the finger everywhere but at thier own code. Normally a process of elimination will prove them wrong It's probably a good idea to start collecting system specs from the users having problems and trying to repro the issue so you can debug it. And get that programmer to fix, SlackeR!
Run the applet separately in appletviewer, and get some GC profiling output (run with -verbose:gc on the command line). It's unlikely it's GC that's causing the issue but there is a case in the AWT to do with images (I forget specifically what, just remember this issue some time ago) which causes a load of large objects with finalizers to start filling up the heap. The problem with the finalize() method is that if it exists the objects aren't collected routinely but only very occasionally, and when they're collected they're much slower than a normal GC. So what happens is the heap fills up fast (esp. if such objects are created in your rendering loop), and extends itself to its maximum size, and then the GC takes abnormally long to deal with it. Cas
Tell your programmer to place timing code around all the major sections of code which can detect these delays. Assuming it's not GC, he should be able to narrow it down to a section of code, and then to a single function call. BTW, On some hardware configurations I've seen the DirectX Present() function delay for *exactly* 10 seconds. I don't know what the Java equivalent is, but definitely concentrate on all draw and render function calls. -Jeremy
Na... no need to put timing code anywhere. Just profile it. You can also inject timing code (via profiling) if you want really accurate figures, but that slowdowns the app quite a lot. The usual "samples" method works good enough and you only get a slight slowdown. java -agentlib:hprof=help Will display the help text. Also try -verbose:gc as Cas suggested.
I don't think it's related to GC. I have the same problem after switching my computer with "Titan Attacks" - i just hangs for a couple of seconds, normally during start up. I've no idea what's causing it, upgrading to a newer java version didn't solve the problem - so I think it's somehow driver related. The same version of TA works nicely on my older box - it's quite strange.
If it's the windows version then, yes, definitely driver related, as Titan for Windows has it's own JVM. To test it out on your native VM you need to hit the Linux (Webstarted) version. Cas
Same behavior using the WebStart version - the main windows starts and then zthere is around 5seconds delay before the puppy games screen with the progress bar.