Jump to content

RAM usage and garbage collection


Recommended Posts

I was reloading my save file a bunch yesterday for nondescript reasons when I had to restart the game due to a slightly increased memory usage.

   mems.png.8407546ebe590c283950d48dab365dfa.png

Yeah, that's 23 gigabibs. I recently upgraded my RAM just for this game, so it was honestly kinda entertaining to watch it fill up.

As a developer myself, I've seen behavior like this before. Managed memory will often not invoke the garbage collector at logical times, meaning any orphaned collections might remain resident in memory longer than they logically should. You may have investigated this issue already, and in both of the situations below my proposed tips and tricks may already be in use by the game. But just in case, I thought I'd offer a couple tips that might help.

It is my understanding that the game is using .NET through Mono. If that's not the case, then oops!

Manually invoke the garbage collector
.NET provides a System.GC static class that comes with a Collect() method. This isn't guaranteed to do the deed, but it may assist in reclaiming unused memory after it's no longer needed. It should be effective as soon the last reference to a large collection is set to null.

My suggestion is that whenever a game save is being loaded, invoke GC.Collect() to help with orphaned objects.

Reuse collections
When the garbage collector doesn't fix the problem, in my experience the next best thing is managing memory myself. Most often this comes from collections such as array lists or hash maps. In .NET, List<T> and Dictionary<Tkey, Tvalue> both have a Clear() method:

My suggestion is that whenever a game save is being loaded, do not create new collections to load into: call the Clear() methods on the existing collections. Most (all?) collections have a capacity under the hood that isn't reset by Clear(), meaning the memory won't be re-allocated unless the new data being loaded into it is larger. This may be a good idea regardless because it may speed up the process.

1 hour ago, GuyPerfect said:

I was reloading my save file a bunch yesterday for nondescript reasons when I had to restart the game due to a slightly increased memory usage.

   mems.png.8407546ebe590c283950d48dab365dfa.png

Yeah, that's 23 gigabibs. I recently upgraded my RAM just for this game, so it was honestly kinda entertaining to watch it fill up.

As a developer myself, I've seen behavior like this before. Managed memory will often not invoke the garbage collector at logical times, meaning any orphaned collections might remain resident in memory longer than they logically should. You may have investigated this issue already, and in both of the situations below my proposed tips and tricks may already be in use by the game. But just in case, I thought I'd offer a couple tips that might help.

It is my understanding that the game is using .NET through Mono. If that's not the case, then oops!

Manually invoke the garbage collector
.NET provides a System.GC static class that comes with a Collect() method. This isn't guaranteed to do the deed, but it may assist in reclaiming unused memory after it's no longer needed. It should be effective as soon the last reference to a large collection is set to null.

My suggestion is that whenever a game save is being loaded, invoke GC.Collect() to help with orphaned objects.

Reuse collections
When the garbage collector doesn't fix the problem, in my experience the next best thing is managing memory myself. Most often this comes from collections such as array lists or hash maps. In .NET, List<T> and Dictionary<Tkey, Tvalue> both have a Clear() method:

My suggestion is that whenever a game save is being loaded, do not create new collections to load into: call the Clear() methods on the existing collections. Most (all?) collections have a capacity under the hood that isn't reset by Clear(), meaning the memory won't be re-allocated unless the new data being loaded into it is larger. This may be a good idea regardless because it may speed up the process.

it does use garbage collector, your info not usefull

this is database type off game, it does use memory, more items you have more memory it uses.

you say you reload game allot, garbage collector does not delete the gameobjects, as there is leftovers from old save after you reload

11 hours ago, degr said:

> this is database type off game, it does use memory, more items you have more memory it uses.

If your savefile is 50 mb, and it contain all information about game state, "database" should also be 50 mb, not 23gb

Thats not entirely right. The savegame just tells the game what to load (and where). The actual load can be x times the size of the actual save file.

For example: Load texture manual generator at x24, y 500. Its just a few bytes in the save file. And then the game loads 500 megabytes of textures and animations.

And then the game loads 500 megabytes of textures and animations

What for? Game should draw only picture which fit into screen, and I'm 100% sure ONI use "sprite" animations, which also should be not a problem.

GUYS, THIS THREAD ABOUT 23GB RAM FOR GAME PROCESS, IT IS DEFINETLY PROBLEM. IT IS NOT RIGHT, THERE IS NO EXCUSE SUCH AS "IT IS DATABASE GAME, IT IS FINE". IT IS NOT FINE.

I may suppose that garbage collection routine starts in moment when game can't allocate more memory for it internal needs. In same time, even after garbage collection finished, memory consumption chart may stay unchanged - because game process allocate 23 GB RAM, then clean it up, but most probably process will not "return" memory back to operation system. Anyway, it is not right, there should be a limit, for an example 0.5-4 GB. There should be a balance - if max memory too high, garbage collection will be heavily time and resource consuming, if max memory too small, garbage collection will be called each moment but consume almost not resources.

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...