Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game starts stuttering *randomly*  (Read 1884 times)
0 Members and 1 Guest are viewing this topic.
Offline Kakashi266

Junior Member





« Posted 2011-07-24 17:38:28 »

I used to think it was random, but not anymore. Basically I have a game that is a top-down, 2d and tile-based. There is smooth animation from tile to tile. At least for the first minute or so. I put the game down for a while because I could not figure out why, after a minute or so, it would start stuttering. Then I noticed while profiling it that once the heap space starts filling up and the garbage collector switches on, then that is when the game slows down.



In the heap space panel, the slowdown happens right after that first steep drop in used heap space.

I've had a few basic computer science classes, but 90% of what I know is self taught, so this is getting to be outside my ability to think through. I just wanted to know if someone here could help me out, give me a kick in the pants for overlooking something stupid maybe? Also I'm not sure what else to provide, but will post whatever is asked for. Thanks.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-07-24 18:23:03 »

It seems you are creating too many objects. The GC is known to cause slow downs for massive memory recall like this. Try reusing and minimizing the number of objects you create.
Do you use Java2D or OpenGL?

Offline Kakashi266

Junior Member





« Reply #2 - Posted 2011-07-24 18:27:09 »

I am using OpenGL. Too many objects, hmmm? I'll see what I can do about that.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-07-24 18:30:30 »

You can try with larger heap size; This is what I do on my aplet  <param name="java_arguments" value="-Xmx350m">
Offline Kakashi266

Junior Member





« Reply #4 - Posted 2011-07-24 18:57:42 »

Adding a larger heap size had some interesting results. It didn't quite stutter like it did before, I'm guessing because it didn't go through some massive garbage collection. However pretty quickly I noticed that there were small little pauses. It's hard to say, but I think they happened every second. Unfortunately my map was randomly generated and did not have the room for me to walk trough more than two hiccups at a time. So an improvement? I suppose.

ra4king is probably right about my objects. When I started the project I was obsessed with making every little distinct thing it's own class. Therefor I have a MapManager object, which holds (for testing purposes) just one Map object. That Map object uses a 2d 50x50 array of Tile objects. I never thought about it at the time but yeah that's 2,500 objects for just one map! For shame me, for shame.

I'm not quite sure how to take all of the information stored in the Tile object and integrate it with the Map object. Assuming I can store it all in array form inside it's Map parent, would that help things? Is a giant, information stuffed single object better than a simple object that stores all the info in other objects?

Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2011-07-24 19:10:44 »

Right now it looks like you have a very small heap size since you're doing a garbage collection at 20M so I wouldn't say you're creating too many objects, its more that you're creating too many objects for the size of your heap.  2500 objects isn't that much so I wouldn't worry about it.  You're running your app in under 20M of memory, which is insignificant compared to the GBs that triple-A games require and computers can handle those fine.

If you're running it as an application, instead of using the applet syntax pitbuller gave, you can use "java -Xmx350m ..." (so very similar).

Also there are many types of garbage collectors and parameters for each collector that you can use with Java.  The default one does a pretty good job at being low pause until it has to do a full collection (which is probably the big drop that you see in the profiler).  When the default collector does a full collection it blocks the program until it completes.  There is a parallel garbage collector called the Concurrent Mark and Sweep collector that I like to use.

It is less efficient but runs in parallel the whole time so the visible stuttering is much reduced.  You can turn it on with "java -XX:+UseConcMarkSweepGC ..." and see if that helps.

Offline Kakashi266

Junior Member





« Reply #6 - Posted 2011-07-24 19:19:36 »

Well lhkbob, I tried it out, and noticed a couple of differences. However, after a moderately sized GC, the game went right on stuttering.

Offline philfrei
« Reply #7 - Posted 2011-07-24 19:28:52 »

I had a memory leak on a program. The program would start out at around 20MB, but it didn't start stuttering until the heap space had grown into the range of 500MB to a gig, depending upon what else was running at the same time. So, I would look elsewhere.

There is a slight bump in the number of threads at the point you said there was stuttering. It is possible that these threads are blocking each other? Do all your threads occur on the swing EDT (in which case that thread might be overworked)? I think a look at thread activity might be a useful step in figuring this out.

Also, I'm curious what you are using for a game loop, and if there are possible tasks there that are causing single iterations of the loop to take longer than the period it is designed to run within.

EDIT: the activity on those threads that pop up and disappear might give a clue as to where blocking might be occuring.

EDIT #2: can also try making a smaller board and seeing if the error still occurs.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Kakashi266

Junior Member





« Reply #8 - Posted 2011-07-24 19:37:03 »

philfrei I'm using FPSAnimator to control the game loop. I'd like to keep experimenting, but I've to get ready for work. I might be on after work to see what others have to say, but will not be coding (programming + vodka = bad).

I do have to say that Threads were never touched upon in the classes I took. What little I know about them is entirely self-taught, and probably encompasses about 4% of what there is to learn.

Thanks for the advice so far, and later folks!

Offline JL235

JGO Coder


Medals: 10



« Reply #9 - Posted 2011-07-24 20:08:45 »

You need to profile the memory usage of your game. Whatever objects your creating loads of, that is the cause of your slowdown.

Common culprits include LinkedList, HashMap, TreeMap, or any in-game objects your creating 10,000s of like background stars or bullets.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #10 - Posted 2011-07-24 21:42:45 »

Also keep an eye out for objects that use a lot of memory - big arrays, buffers, etc. being allocated on every frame - they can definiely be worth reusing.

A slight extension to lhkbob's suggestion - when doing low latency audio work where the margins are much tighter and less forgiving, I use

1  
java -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing


works well for minimising GC pauses.  NB. The last option may not be needed as I think it's now the default on Java 6 when specifying the first two.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2011-07-24 23:54:05 »

Just -Xincgc works fine for me.

Cas Smiley

Offline Kakashi266

Junior Member





« Reply #12 - Posted 2011-07-25 02:55:23 »

Thanks these are good suggestions. I do remember Hashmaps being used a lot, when I profiled the game. I'll have to track those down. I have buffers that hold the vertex and texture information, assuming those are included in what you meant. I don't allocate them every frame. Just the few I start with that are reused (info deleted by standardized delete methods and simply repopulated). That stuff doesn't happen every frame, either.

Anyway, I'm still keeping an eye on this thread tonight, to spot any new ideas. Thanks again.

Offline JL235

JGO Coder


Medals: 10



« Reply #13 - Posted 2011-07-25 07:38:12 »

There are alternatives to hashmaps out there, I have my own CachingHashMap which I use instead, but there are faster options around. So it could be as simple as just swapping out the HashMap for an alternative one, and that's it!

Offline Kakashi266

Junior Member





« Reply #14 - Posted 2011-07-25 17:45:58 »

Well I've done some digging and it seems that a lot of the generated objects are a bit outside my control, byproducts of the greater complexity involved in using JOGL and font rendering and so on.

Some of it may also be caused by un-optimized JOGL. I'd say 20% of all the time I spent learning Java was dedicated to wrapping my head around VBOs, and shoehorning them into my engine. Basically, I believe I've got it right, just not optimized.

The good news is that for some reason my game has settled down. I tossed -Xincgc in there, and I'm not sure if it helped, but seems like it might have. There are just the occasional pauses when a larger collection occurs, but I ran the game for over 4 minutes with just a scant few hiccups. As this game is just a personal project to share with friends, I can live with those. Time to get on with it!

Thanks for the help, folks!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (17 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!