Advertisement

[java] Efficiency & Memory

Started by March 27, 2001 06:19 PM
10 comments, last by Flanders 23 years, 10 months ago
Hey gurus I''m writing a little chess program - bascially all I want to do is mess around with some AI and have a little fun. I have the actual board and moves and even some simple AI working, but one thign is really stopping me from progressing. My recursive algorithim basically does this: 1. Get the next legal move 2. Take a copy of the board 3. Do the move we got in #1 4. call this funtion recursively to a previously defined limit 5. Go back to 1 The problem is that I run out of memory before I can finish even a 2 ply ( basically 4 moves ) search! At this point I''m quite happy with how fast it is searching, but the memory issue is really putting a damper on the whole thing Basically my board is a 8x8 array of ints with a class called moveList attached to it (to keep track of the game history) - now I can move the game history off of the board and into another class, but at this point it is basically empty anyway (since this is the first move of the game , and the MoveList class is basically a doubly linked class - very small to start) So my questions are these: 1) Is there a way to force the Java VM to clean up memory? (It should do this automatically, but maybe it isn''t in the middle of a recursive call?) 2) My code to copy the board is like this: public Object clone() { Board bCopy = new Board(); ... double for loop to copy the int array ... copy a few other booleans.. } - Is this costing me big time? Perhaps I''ve overloodek something major? Your help is really appretiated! I''m really enjoying this program, and if anybody is really interested I can clean up the code and post it. Thanks! - Jim
- Flanders -
So how many seperate moves (i.e. states off the board) do you consider? I guess, you have 10 initial moves per side
plus I guess at least another 10 moves per side (on average) after that. So for 2-ply that is 10*10*10*10 (10k) combinations,
if you have a board for each one (each 8*8 board of ints takes up
8*8*4 (256 bytes) at least) so thats 256*10*10*10*10 (2M), which should not be too bad. But java will store a few more bytes than just that. Although are you running out of stack or heap space? I can never quite figure out the difference in java, but I know that stack won''t be that big (640K, maybe).

The best thing to do is to free objects up for garbage collection as soon as possible. Once you have scored a board you don''t need to store the whole board, just the move(s) it takes to get there/and/or it''s score.
You can call System.gc() to force garbage collection, and you can find out the amount of free memory by doing:

Runtime rt = Runtime.getRuntime();
long freeMem = rt.freeMemory();

Which might be handy for figuring out excactly when the memory disappears.
That''s about all I can think of at the moment, hope it helps.

cheers,
John
Advertisement
I''ll post the actual error message when I get home tonight...

Does garbage collection take a long time to execute?

I have 20 moves on each side for the first move - 16 for pawns 2 more for each knight - so teh number of boards for 2 moves (one white one black ) is actually 400... for the next 2 moves there''s around 20 (some bishops & queens have more range... so +/- 2 or so on average I guess ) so we''re looking at approx 20*20*20*20 = 16*10k ... and then 256*16*10K = 32 M I guess? (by your calcs?)

Anyway, any more suggestions are appretiated - but I''ll definately try freeing up some memory whenever I can. THANKS!
- Flanders -
Garbage collection shouldn''t take _too_ long, System.gc() will return practically straight away, but it might not have done anything (it''s merely a request). So if you _know_ that you have garbage to collect you can sit in a while loop untill you have more free memory.


>so we''re looking at approx 20*20*20*20 = 16*10k ... and then >256*16*10K = 32 M I guess? (by your calcs?)

Ah. If you are using up 32M, that could be your problem. The java heap defaults to being 16M big (at least it did, I think newer versions of java seem to deal with larger heaps automatically). So you might need to run java with the -mx option, e.g. java -mx32M would run java with a 32M heap. Check your java documentation to find out more.

How many times per game do you think you will copy the board? By creating a new object everytime you copy your object you could be running out of memory right there.. See if there is a way to recycle the object, maybe have a pool of one or more of the objects that can be recycled. Just a thought...

Andy

War doesn''t determine who is right, war determines who is left.
I think that I can improve the algorithim quite a bit by reusing some boards over... this was just meant to be an experiment to see how well a recursive algorithim worked. I''ve solved my memory issues so far... however it''s really slow when I try to search 6 moves down!

My next algorithim will try to fix these issues I think. Most likely I can use either one board or maybe one board / move ... it would save recreating the object for each level of recursion. -- this is getting into an AI forum post as opposed to a Java forum post though I suppose

Thanks for the input, expect more questions from me!
- Flanders -
Advertisement
So are you doing a full 6-ply search? That''ll get massive, in an exponential way, very quick. You should probably start using heuristics, and A* (plus alpha beta pruning), that should help you minimise the amount you have to search. It should definately help when there are only one or two obvous moves to do.

John
Can you suggest a good place to take a look for A* ? I have no idea what it is! I''ve heard the term before but never actually looked it up or how to implement it...
- Flanders -
A* is the "standard" search algorithm used in games. Basically you have whats called a heuristic (from the greek to search), which is used to approximate how "good" a particular board is.
The heuristic is used to decide which board you want to evaluate first, ie the best. The heuristic has two parts, the cost so far (from doing several moves to get to the current board) and a proper guess at what state the board is in.

A* is often used for path-finding in tile based games, and the heuristic for that is normally the distance travelled so far and the guessed distance to the target.

Look at:

http://www-cs-students.stanford.edu/~amitp/gameprog.html

for a guide to A*, it''s mainly to do with path-finding, but it should give you a starting point. You should be able to find plenty of good heuristics for chess, as it''s a well studied problem in GOFAI (good old fashioned AI).

cheers,
John
Thanks, I''ll be sure to check it out!
- Flanders -

This topic is closed to new replies.

Advertisement