Memory management issue (making a game)
in
Programming Questions
•
8 months ago
HI.
For days, I've been trying to make a not-that-very-simple game.
I started with making some basic interfaces and positioning them without actual function.
It had been so far so good until I noticed that the program takes too much memory, over 600MB often.
To compare, I played Braid, which never exceeded 300MB of memory all the time.
Originally, I coded the program to load up all data at the beginning and never throw them away.
Since I thought this had been a problem, I modified the program to load and throw away data according to the in-game phase.
Say, there are two phases for the game: MENU and WORLD.
In modified code, all needed data for MENU are loaded just before executing MENU.
If you wish to go to WORLD phase, data for MENU are nullified and data for WORLD are loaded before executing WORLD.
Changing phase from WORLD to MENU makes the same process in reverse.
However, it seems like that nullifying variables and objects is never working in terms of memory problem.
The memory never be freed up, while loading data always increases memory usage.
What this means is that if you repeat to go to MENU and WORLD on and on, memory usage just soars, which make the program crash or the whole system restart in the end.
I googled the problem and found that there's something called garbage collector.
I tried to manually call it("System.gc()") knowing that it is not a good idea for most times.
Frustratingly, this didn't seem to work also.
I wonder what is the problem and how can I fix this.
I just cannot concentrate on making the game further because without having the problem solved, doing more would only worsen the problem.
Note that I'm not familiar with any other language including native java, and never have dealt with memory problem so far, which means I'm nearly ignorant about these memory things.
The entire code can be downloaded through the link below:
THANK YOU IN ADVANCE!
Just do not doom me and the project with skeptic view please.
1