[ian-reeds-games] Re: Memory problems

  • From: Ian Reed <support@xxxxxxxxxxxxxxxxxxx>
  • To: ian-reeds-games@xxxxxxxxxxxxx
  • Date: Mon, 22 Apr 2013 22:18:19 -0600

Hey Carlos,

Thanks for the numbers on memory usage. It did get me to realize that I was keeping a reference to all unit, item, skill, and effect objects as well as all their sub objects and never cleaning them up. This happened when I started keeping a single dictionary that had a reference to every object based on it's ID. That was to support multiplayer so that in memory objects could be referenced across machines using only the ID.
Then I forgot to clean that dictionary out.

As this project supports more and more scenarios it becomes easier to make mistakes like that.

I've made a fix for that at least.
I won't say it solves all the memory issues but it should help a fair amount.

Currently the old map gets cleared out whenever you load a new one so taking the user to the main menu instead of the map pack menu won't make a difference. But I still think that's a good idea as it's annoying to have to get back into the map pack menu, and Allan mentioned it a while back as well.
Whenever a map loads all information from the map pack loads again.
Originally I thought it would be neat as you say to keep old info in memory so the entire map pack doesn't have to be reloaded as you switch maps but eventually I just had everything reload when you load a map because it was simpler and more straight forward.
There are other ideas for improving the speed of a map loading.
For instance, I currently load all map pack music and sounds into memory whether the map pack uses them or not. If I changed this code to load them on the fly then initial map load times would be much better.
Just another thing on my list, smile.

Allan, I wouldn't worry about converting from .wav to .mp3 unless you want to.

And now if I may ramble to no one in particular.
This app will never be completely free of memory leaks. I think that must be impossible. When I was reading up on why Allan was getting out of memory exceptions while saving repeatedly I found a number of people stating that the standard .NET Serialization had some native memory leaks. If something so standard as that with a huge QA team and multiple devs still has memory leaks then I'm afraid we're just stuck with them in my games, smile.

But of course I'll still do my best to keep them down to a level where they don't slow a good machine like Carlos has.

Another thing that makes it difficult to be sure about memory leaks is that I believe the number we see in task manager includes the entire memory footprint of the application. Since C# uses a garbage collector and maintains a managed heap that is larger than the actual amount of memory you currently have allocated it can be difficult to accurately determine if the app is actually leaking memory or if the managed heap is just growing due to fragmentation as you allocate memory and the GC frees it up. Additionally since the GC tries not to run unless it needs to there's still plenty of opportunity to see a high number even after I've cleared out references to unused objects. But if you can repeatedly make it grow by large jumps then it's worth looking into as this was.

It really comes down to making it good enough, since it will never be perfect, as much as I'd like it to be, smile.

Again, I appreciate hearing about whenever there seems to be a very large leak as that one.
I hope it's better in the next dev release.
I wonder if Jarek and Mohsin's problems could have been related.

Ian Reed



On 4/22/2013 6:22 PM, Carlos Macintosh wrote:
I was running tb and playing Allan thompson’s starship battles map pack. It as being all laggy. I am using a computer running a 2ghZ core 2 duo processor with 3GB of ram. That’s fairly speedy. Now, when I started Allan thompson’s map pac, and played through a couple maps, the game began to get all laggy. Like it took a second or two to process commands. I' don’t think that the map pack is the problem, but the handling of resources. Even just on the main menu at game startup the game consumes 46mb of memory. When I loaded up tomb robber, the memory usage jumped to 80 mb. When I returned to the main menu quitting tomb robber, the memory usage stayed at 78mb. That in itself is strange, unless you never unload the material used by tomb robber, which is my theory. Well, then I decided to start starship battles reloaded on mission 1. It took a while, and when the map loaded memory usage had spiked to 158 mb! Upon returning to the main menu, memory usage was still 152 mb. I loaded up a second map from the starship battles pack, and, again, the memory usage spiked to 234 mb. My theory is that it loaded up all the sounds and music, ALL OVER AGAIN, which contributed to the map’s glacial start time. And it never unloaded them, which spiked the memory usage. So, after playing 5 or 6 of those star trek maps, and loading 5 or 6 sets of the 94 or so mb of sounds and music, memory usage could go well above 5 or 600 mb. This is not a good thing, and, as map pack sizes grow, the damage will be more pronounced. I suggest the following: instead of returning a user to the main menu upon victory or defeat of a map, return them to the menu containing all the maps in the current campaign. If they press escape to close that menu, unload everything about the campaign. Also change the option on the battle menu from “return to the main menu” to “Return to map selection” and return the user to the map selection menu. This way, time would not be wasted loading and unloading the same campaign material whenever a user stoped playing 1 map of a campaign and switched to another. Plus, most users, as they play a campaign, will go to the next mission. So it saves time as well. I hope this is helpful, and not just annoying
Thanks
Carlos

Other related posts: