Author Topic: Too Much Heap Sections ???  (Read 20037 times)

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Too Much Heap Sections ???
« Reply #30 on: March 16, 2011, 11:28:22 pm »
Well, in this case the answer is fairly straightforward:

We normally have a class called ForegroundUnitType, which stores all of the data that is per-ship-type per-player.  Some of that stuff varies by player from things like handicap or if they are AI or human (like description text for that, mainly), and some of it varies by ship cap scale.  It also included an even larger number of things that never change per-ship-type, including huge arrays of stuff about which ships can hit which other ships, etc, but these were getting duplicated 10x, since there are 10 player slots.

The problem is, this class was absolutely huge, and included a huge amount of text, too.  What had started out as a really efficient way to do things, over time, became partially inefficient because of the introduction of those variances like ship cap scales, etc.  That really messed with things more than I realized, when it came to RAM use.

My solution was to split ForegroundUnitType into two parts: ForegroundUnitType and ForegroundUnitTypeImmutable.  The second class includes stuff that is per-ship and which will NEVER change throughout the life of the program.  Not based on player, not based on ship cap scale, nothing.  This was... by far the bulk of the data in ForegroundUnitType.  All the rest of that stuff that DID vary, I left in ForegroundUnitType.  Then ForegroundUnitTypeImmutable is only ever instantiated once per ship class, right at program start, and then every ForegroundUnitType of the corresponding type references the immutable class as a sub-object that lets all the semantic relationships remain largely the same.

The reason that this wasn't an obvious optimization, now or in the past, was that the individual objects are so small that they don't show up in the profiler from Unity.  Partly that's because their profiler is... ahem... not that great, and partly because their deep profiler crashes when profiling a game as large as AI War.  Because it's trying to cache all its results data in memory.  Since this memory explosion was post-unity switch, and so diffuse, we didn't find it.

What started getting me thinking about this, however, was the fact that AVWW has SO much going on onscreen and such, but is only using between 20-50 MB of heap at any given time.  Right after program start, lately AI War has been using about 250MB of heap, without even loading a savegame, just from instantiating all of the metadata about ships, loading in the localization files, etc.  That seemed... way too high, and ForegroundUnitType was the most obvious potential culprit.  But I was still really taking a leap to even spend a few hours working on that, because it could have led to not much gain and just a few hours wasted and code reverted back.  

Frankly I'd hoped to save about 50MB, but on program load it's actually about 190MB from what I'd been seeing directly prior to starting this optimization.  It's only using 60MB of heap when loading the program now. :) Out of a pool of 900MB, that's pretty huge.  After loading Shadoz's savegame and then stopping the unity editor, then restarting the game, I'd get a heap error every time (the unity editor keeps heap somewhat in use between runs, unlike the fully compiled game).  Afterward, it doesn't even blink.

Win. ;)
« Last Edit: March 16, 2011, 11:38:35 pm by x4000 »
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Too Much Heap Sections ???
« Reply #31 on: March 17, 2011, 12:03:54 am »
Normalization of data FTW!

(Yea, that is a database design concept, but the concept of moving repeated but identical data to a separate area where it is only stored once most certainly describes what you just did)

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Too Much Heap Sections ???
« Reply #32 on: March 17, 2011, 12:06:05 am »
Yes, I try to keep everything in third normal form even in a non-relation environment.  T-SQL was my lifeblood for a good 8 years, and it's still possibly my favorite language.
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Too Much Heap Sections ???
« Reply #33 on: March 17, 2011, 12:11:52 am »
And this release is now out: http://www.arcengames.com/forums/index.php/topic,8435.0.html

Shadoz, this should solve your problem simply by you upgrading, but if the problem persists, then I suggest the new Don't Compress Savegames option on the Advanced tab of Settings as the next step there.  But from what I'm seeing, even that shouldn't be needed!
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline Shadoz

  • Newbie Mark III
  • *
  • Posts: 47
Re: Too Much Heap Sections ???
« Reply #34 on: March 17, 2011, 12:08:35 pm »
very gib thanks to x4000 ... great work :-)

it helps extremly ...
i play for 2 hours now and dont have had any heap section ... at this time ... looking up what happends if there is more action

maybe there is the one or other idea to continue the RAM handling to get little more power in future ...
and hoping the unity engine developer make some more possible :-)

but for this time iam happy


after starting the game befor 5.004 i had 1,1 GB instant at RAM by loading savegame ... after 5.004 only 0,95 GB and it increase slower than before

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Too Much Heap Sections ???
« Reply #35 on: March 17, 2011, 12:09:15 pm »
Excellent!
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline Shadoz

  • Newbie Mark III
  • *
  • Posts: 47
Re: Too Much Heap Sections ???
« Reply #36 on: March 18, 2011, 05:32:16 pm »
And a last one again :-)

big thanks excellent great work ... played today the whole day and dont get any heap section ... thats very nice ...

but you know too that there is such more potenzieal at having more at once during high chip caps bigger handicap ++ and with starting more homeplanets ...
i should try a haeavy game with this new RAMinator *gggg*


BTW is there any way to improve the CPU too to get better handling ?? i know there was in past some very nice improvments to it :-) but maybe there is something more to do ... i mean its a extreamly high scaled game ... at least to its theoreticaly potencial :-)

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Too Much Heap Sections ???
« Reply #37 on: March 18, 2011, 06:27:30 pm »
Glad it worked out!

Bear in mind this game is already highly optimized.  If there were more things to do, we'd have done them.  As it is, it beats pretty much every other game simulation on the market in terms of scale fairly hands-down. ;)
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!