Can anyone teach me how to debug Java codes, however messy it may be?

Started by
6 comments, last by the mole 10 years, 3 months ago

I have finished implementing A* pathfinding in my game. Here's the situation: (If I don't give a situation, everyone's going to give generic contexts and generic answers.)

Before implementation: The game is running smoothly at 60FPS. The game itself is simple, all thanks to easy grid movements for moving entities.

After implementation: The game is now lagging badly and it's unacceptable. The only thing I know that's causing the lag is the Map of nodes, in which the size is 160x120. Every 256 + X game ticks, once a unit entity spawns, it calls upon the pathfinding system to create a path that lets the unit entity to go from point A to point B. The X is arbitrary, since the player has to click on the screen to place down the spawners, which they automatically initiate themselves. At 60FPS, that's about every 4 to 5 seconds, the pathfinding system starts execution, which may lasts several seconds, due to the size of the nodes.

I heard many things about debugging code, such as profiling, logging, and simple step in/step out with breakpoints. Of the first two, I only know how to debug codes with the last option. Perhaps maybe if I do profiling, I might be able to see where the game is lagging, and then ask for more help on what I should do to solve it.

Of course, the most obvious solution is to cut down the number of nodes in the map. Unfortunately, each node is also binded to a color value. All nodes are pixel data taken from a BufferedImage, or in this case, a Canvas. So, I can't just edit out the size. Otherwise, I would have to do a complete rewrite, and I want to avoid that by all means, unless it's absolutely necessary. Speaking of A*, maybe I could adapt the algorithm so that it uses a greedy method of approach, but I don't have the knowledge to do such a thing, let alone tweaking the heuristics. I'm such a noobie here.

And how do you supposed to know when you're supposed to delegate the task (A* algorithm) into a concurrent task, and how to go from there? I don't have that much experiences with concurrency, so I hoped to hear what experienced experts would say about this.

Advertisement


All nodes are pixel data taken from a BufferedImage, or in this case, a Canvas.

Java attempts to keep BufferedImage objects on the video card. If you read from a BufferedImage it can change to software rendering, which is SLOW.

That is only one of many things that can cause Java to switch from hardware acceleration to software rendering. If you want high performance graphics you cannot use Java's built-in graphics system. Use something like JOGL instead that is always hardware accelerated.

I'd have a different structure for path finding. An enum matrix for example (with constants like StateEnum.SOLID, StateEnum.AIR, StateEnum.WATER, etc). Or, if you want the traversing as fast as possible, maybe a matrix of ints and some tag values (0 air, 1 solid, 2 water, etc).

There is no need for the pathfinding to be attached to the actual pixel values.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

The debugger is your best friend as a programmer, learn to use it :). But there's other ways too, like dumping stuffs in a file, using messagebeep to know if some code is executed, or simple messages print in the console.

A naive approach: set up a chronometer to test how much time is spent by a procedure each frame. Maybe it can helps you find the real bottleneck. Log values on the console or on a file. I'm pretty sure you have not to reduce the size of the map, but improve your current implementation of A* instead.

About debugging:

It will help to know what IDE are you using to dev.

If you are using eclipse: The IDE has tools to help you with debug and profile. ( Right click your main file - the one with 'main' method - and choose Profile As... or Debug As... )

If you does not want to use your IDE Resources, you can use the JVisualVM ( JDK_HOME\bin\jvisualvm.exe ). This app is a visual tool to help you measure your memory consumption, alive threads, etc etc... you can even take a screenshot of you heap to see what objects are using more memory, etc etc... This tool also have the "Profiler" option. Its simple to use... Just click on Profile, start CPU measurements... play your game for a while and get back to "Profiler" and stop the measures... You will be able to see how much invocations each method has, how much time spent, etc etc.. so you can focus on you major time consuming methods and try to find a way to reduce its time...

Well... if you want to be serious about improving java performance I recommend the book "Java Performance" ( http://www.amazon.com/Java-Performance-Charlie-Hunt/dp/0137142528 ) it give you a "Big Picture" about common mistakes made by programmers, and how to avoid it, also how to use the right tools to find your application bottlenecks.

Hope it helps... there is a lot of material on google about how to profile and maybe you can find good material on Oracle´s website.

KrinosX


If you does not want to use your IDE Resources, you can use the JVisualVM ( JDK_HOME\bin\jvisualvm.exe ). This app is a visual tool to help you measure your memory consumption, alive threads, etc etc... you can even take a screenshot of you heap to see what objects are using more memory, etc etc... This tool also have the "Profiler" option. Its simple to use... Just click on Profile, start CPU measurements... play your game for a while and get back to "Profiler" and stop the measures... You will be able to see how much invocations each method has, how much time spent, etc etc.. so you can focus on you major time consuming methods and try to find a way to reduce its time...

Yes, give JVisualVM a try. If you can grab a profile during the time your game is frozen, you'll at least be able to post a more specific request for help.

Besides the debugger and jvisualvm which have already been pointed out, I would like to throw "Logging" in.

You should log stuff in your application and you should never System.out.println or System.err.println.

You should always wrap the log output by a Logging system (e.g. Java Logging Framework, or SLF4J).

There should also not be a "e.printStackTrace()" somewhere in your code.

Logging also helps to find setup problems, because 3rd part libraries/frameworks usualy complain about problems.

This topic is closed to new replies.

Advertisement