Will too much memory usage become a problem in games?

Discussion in 'Javascript/Plugin Support' started by Tsukihime, Oct 29, 2015.

  1. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,062
    Location:
    Toronto
    First Language:
    English
    I'm looking at the the way ImageManager is implemented and it uses a cache to store objects. Example

    ImageManager.loadNormalBitmap = function(path, hue) { var key = path + ':' + hue; if (!this._cache[key]) { var bitmap = Bitmap.load(path); bitmap.addLoadListener(function() { bitmap.rotateHue(hue); }); this._cache[key] = bitmap; } return this._cache[key];};Since this is meant to be the cache, it's meant to stay around for awhile.As you load images, they get cached.

    At some point, if you load lots and lots of images, you're going to end up using a lot of memory.

    I'm sure eventually you're going to run into problems?

    I don't know if audio or other resources have a similar issue since I haven't looked at those yet.
     
    Last edited by a moderator: Oct 29, 2015
    #1
    RetroNutcase likes this.
  2. RetroNutcase

    RetroNutcase Jolly Cooperating Sunbro Veteran

    Messages:
    63
    Likes Received:
    17
    First Language:
    English
    I've been wondering about this myself. Especially considering mobile devices can run into memory problems quite easily.
     
    #2
  3. bgillisp

    bgillisp Global Moderators Global Mod

    Messages:
    12,098
    Likes Received:
    12,279
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMVXA
    You are probably right. Though, I've noticed many programs don't seem to care about memory allocation anymore, as they think everyone has all the memory in the world, so why bother?

    And that is also why I find I now have to close and restart my internet browsers about every 4 hours, else they are using up 500K memory easy.
     
    #3
  4. RetroNutcase

    RetroNutcase Jolly Cooperating Sunbro Veteran

    Messages:
    63
    Likes Received:
    17
    First Language:
    English
    With how much Ram you can easily stick in a desktop nowadays, I think people have gotten lazy in memory optimization yes. Regarding Hime's question though, I have to wonder...Would there be any way to purge unused assets? Or load them on a per-map/per battle basis? Admittedly yes, it would probably result in load times, but with low memory devices like mobile phones, this would pretty much be a requirement for anything that used a lot of resources.
     
    #4
  5. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    922
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    I suppose if you wanted to implement a cache-collection system, you should be able to implement a timer that loops through the cache periodically, say maybe once per ten seconds, but probably longer, maybe only on map/scene change, go through each item, while maintaining a list of the last time each file was accessed by aliasing the load functions and storing Date objects in a list for each filename. If the last time a file was accessed is greater than a defined threshold, destroy the cached copy.

    Now, the method I just described is a bit of a dumb method. It doesn't know the amount of memory available, how much it's necessarily using (though you can easily calculate that by checking the length and width of each image as you loop through, multiply them together, and then multiply by 4 for total bytes of memory used (mult by 4 because each color is rgba)), nor how much i sbeing requested. One possible way you might be able to get information on how much is available and how much is used is through chrome://memory-internals/, which has a little 'update' button that will check chrome's memory usage, and then have a JSON representation available in a small textbox, as well as displaying it on the screen. If you can find a way to press the button pragmatically and then grab the JSON output, you should be able to compare available memory to used memory and remove files accordingly. However, in this case you'll have to keep in mind that this will only work on the chrome web browser, the desktop clients, and maybe android ports. Other browsers and iOS would have to be switched to 'dumb' mode.

    But if you have any other ideas, I'm interested in hearing them, as creating a 'dumb mode' cache cleaner wouldn't be hard at all. 
     
    #5
    Tsukihime likes this.
  6. Hackerham

    Hackerham Developer Developer

    Messages:
    82
    Likes Received:
    68
    First Language:
    Russian
    Guys, Its worse than that. Removing _cache wont help at all, because you have to manually free them from webgl context.
     
    Last edited by a moderator: Oct 29, 2015
    #6
    Kane Hart likes this.
  7. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,062
    Location:
    Toronto
    First Language:
    English
    It doesn't sound too bad. It hits two points of using a cache

    1. The last time it was accessed

    2. How often it's accessed

    And you clear out anything that's too old or too rarely used.

    Still better than just keeping it in cache all the time :p

    Web browsers also have their own caching mechanism, so even if something is cleared from the game cache, it shouldn't require users to send a request to some remote location just to get the file again unless they explicitly disable caching on their system.

    I'm looking at some of the scenes and they don't do any clean-up

    Scene_Base.prototype.terminate = function() {};Scene_Map.prototype.terminate = function() { Scene_Base.prototype.terminate.call(this); if (!SceneManager.isNextScene(Scene_Battle)) { this._spriteset.update(); this._mapNameWindow.hide(); SceneManager.snapForBackground(); } $gameScreen.clearZoom();};From the sounds of it, you need to explicitly call some sort of `free` or `destroy` methodLooking at this page, you have to manually call destroy for sprite/texture objects, but when I run a search through the rpg_sprites.js, there is no reference to destroy.

    So, memory leak?
     
    Last edited by a moderator: Oct 29, 2015
    #7
    Amuseum and Hackerham like this.
  8. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    922
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    Without checking it and just thinking about it, I don't really think so. It doesn't free any resources, but it also doesn't recreate them. Once they're in the cache they stay there. If the same files are needed and were loaded earlier, they'll be available in the cache. Similarly to how  worked in Ace, disposing a bitmap from the cache caused issues because the cache returns a reference to a stored bitmap, not a copy of it. freeing your copy will completely wreck any other attempt to get the same image.
     
    #8
  9. Hackerham

    Hackerham Developer Developer

    Messages:
    82
    Likes Received:
    68
    First Language:
    Russian
    Yep, every time Spriteset_Map is created, it creates two textures of screen size that wont be freed if renderer uses webgl context.
     
    #9
    Amuseum likes this.
  10. Ramiro

    Ramiro Now with an army of Mecha-Ralphs! Veteran

    Messages:
    860
    Likes Received:
    366
    Location:
    Argentina
    First Language:
    Spanish
    when you use goto, woudn't the gc actually release the objects ?
    The scene is not stored anymore anywere...

    But yes, they should have freed resources anyway
     
    #10
  11. Hackerham

    Hackerham Developer Developer

    Messages:
    82
    Likes Received:
    68
    First Language:
    Russian
    Garbage collector doesnt work with webgl objects,we need to call destroy() method on every texture/framebuffer manually.
     
    #11
  12. tati light

    tati light Veteran Veteran

    Messages:
    160
    Likes Received:
    108
    First Language:
    English
    Interesting topic, I will have to keep this in mind in the future when my project gets bigger...

    in AIR I controlled use of memory with dispose method, I'm sure in MV there is a way to control that, since there are no hidden classes, and one way or another you can "dispose/destroy" or w/e term they use here...

    or maybe the game already has some kind of cache limit which auto-cleans itself above a certain threshold?
     
    #12
  13. Ramiro

    Ramiro Now with an army of Mecha-Ralphs! Veteran

    Messages:
    860
    Likes Received:
    366
    Location:
    Argentina
    First Language:
    Spanish
    Then as of here.

    This may patch it:

    Code:
    (function () {function _generalDestroy =  function() {    for (var x in this) {      // we should not free bitmaps automatically because the cache loader may mess with us,       //we would have to destroy them manually      if (this[x] && this[x].destroy && (!(this[x] instanceof Bitmap)) ) {         this[x].destroy();      }    }    if (this.children) {      this.children.forEach(function (i) {            if (i && i.destroy) {            i.destroy();          }       });    }} Bitmap.prototype.destroy=_generalDestroy;Sprite.prototype.destroy= _generalDestroy;TilingSprite.prototype.destroy = _generalDestroy;Window.prototype.destroy= _generalDestroy;WindowLayer.prototype.destroy = _generalDestroy;Tilemap.prototype.destroy =_generalDestroy;Spriteset_Map.prototype.destroy = _generalDestroy;Spriteset_Battle.prototype.destroy = _generalDestroy;Scene_Base.prototype.terminate = _generalDestroy; Scene_Base.prototype.terminate = _generalDestroy; })();
     
    Last edited by a moderator: Oct 29, 2015
    #13
  14. Hackerham

    Hackerham Developer Developer

    Messages:
    82
    Likes Received:
    68
    First Language:
    Russian
    As I said in github, Im not sure yet :)

    Guys, whoever want to participate in that fix - please join that thread in github. I'm sure we'll find solution for both vanilla and pixiv3 versions.
     
    Last edited by a moderator: Oct 29, 2015
    #14
  15. Ramiro

    Ramiro Now with an army of Mecha-Ralphs! Veteran

    Messages:
    860
    Likes Received:
    366
    Location:
    Argentina
    First Language:
    Spanish
    They probably won't have a github account.

    Most of them don't want/need one.

    But as I told you before, you should try to make other people write as less as they can, and they only use between scenes Bitmaps and Audio files...
     
    #15
  16. Iavra

    Iavra Veteran Veteran

    Messages:
    1,797
    Likes Received:
    856
    First Language:
    German
    Hmm, looks like this would mess with my popup plugins. But i thought about switching to an observer pattern, anyway.
     
    #16
  17. Amuseum

    Amuseum Veteran Veteran

    Messages:
    71
    Likes Received:
    66
    Location:
    San Francisco
    First Language:
    English Chinese
    Primarily Uses:
    RMXP
    I already encounter out of memory issues on smart phones and tablets. My game is pretty small, just a few small rooms and one big overworld map. Both use Firefox browsers (couldn't get it to run on Chrome-based browsers.) The crash usually happens when switching maps.

    Large games with many resources shouldn't be a problem in itself. Many Android / iOS games are huge, sometimes up to a 1 GB. And they don't crash or get out of memory issues even after hours of play. My simple RMMV game crashes after a few map transfers. So you guys are right. It's either RPG Maker's map handling or WebGL's limitations.
     
    #17

Share This Page