Unloading data from the cache, freeing up some RAM

Hi all,

Is there a built-in way or recommended approach the flush the cache in Phaser? Json, tilemaps, music, etc. to keep the memory footprint as low a possible.

My little phaser game is running perfectly on regular devices (Android, iOs, iPad, pc, etc.) and on most KaiOs feature phones as well (such as the Nokia Flip 5520, see picture).

These low spec KaiOs devices have very little ram; normal ones have 512megs (leaving about 290megs for the game to run). The entry level ones however only have 256megs of ram. It leaves about 30megs for an app to run. I had found and fixed a memory leak (pr) in a tile animator plugin but that doesn’t stop crashing when switching scenes (involving loading music & co).

Thanks in advance for your attention!

edit

I’ve tried the following. It does clean the Phaser cache, but I see the network tab of Chrome still obtains items from its browser cache.

  private onShutdown() {

    // Clean cache on lo spec
    if (environment.type === "kaios") {
      this.cache.audio.getKeys().forEach((key) => this.cache.audio.remove(key));
      this.cache.tilemap.getKeys().forEach((key) => this.cache.tilemap.remove(key));
    }

I had hoped to see an improvement with the following. Doesn’t help forcing the browser to reload the assets:

<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="pragma" content="no-cache" />

Any leads welcome!

If you figure this out, let me know!

To my knowledge, it’s impossible to tell the browser to run any kind of gc or memory flush. It will just do it whenever it feels like it :frowning:

Thanks Rich. I’ll also keep you posted once I get a new build tested with these Phaser-level cache flushing in place; The 256megs device needs to be rooted for me to tinker with its memory on the fly.

Hi @rich … so far, no success. The device still crashes in spite of clearing the cache between scenes.

I’m trying to root the lo-spec phone I bought but it needs some work first. Re-flashing, installing custom tools, etc.

Again, keep you posted!

One thing I noticed when testing this, is that the browser will run major gc when it detects it is mostly idle.

It’s possible this never happens on a low-spec phone, because of the cost of the game loop, rendering, etc.

You might be able to invoke it by freeing-up the browser from processing anything. Literally pause the game, rendering, etc so Phaser is doing nothing and the browser is idle. Then it may try to run gc, freeing up your memory.

Good idea. Just tried that as I could re-flash the device. Adding a 5s pause after clearing caches and preloading but that didn’t help.

All that works so far is to disable the music. I can’t settle for this as it works fine on 512megs KaiOs devices… and there is no memory detection at runtime so this isn’t an option really…

I’ll try to dig deeper in the WebAudio api and see if anything could help.

You may need to ensure RAF stops as well. Like, literally everything Phaser is doing stops for 5 seconds. But sadly, even that may not be long enough :frowning:

Good point. Just made sure anim frame isn’t invoked, didn’t help indeed.

Also, I jsut read WebAudio leaks memory as can keep internals refs to some large sounds. So for kaios targets only I disabled webAudio.

I’ll give it more testing and will post the results!

Erratum: HTML5 Audio doesn’t work on KaiOs. No crash, but no sound. The low spec (256megs) device is just too weak to handle music and a game.

Seeing some bug (link) I gave Howler a try while disabling Phaser’s audio engine. Same problem, crashing when shutting down / preloading / creating World and playing music. Damn.

Currently fighting with the outdated / bogus APIs like navigator.getFeature(‘hardware.memory’) which isn’t working so far. Last trick that could save the day on that platform.

Well, that was off topic. Still, I have great hopes to see KaiOs as a great target for just any Phaser game :slight_smile:

Ok @rich , I give up… tried to replace some internals in Phaser to use MediaElementSource instead of AudioBuffer (as suggested in the spec for long sounds). Doesn’t help either. The 256megs device barely leaves 25-30 megs of ram to the game and there’s no more time I can spend on this (not being snobbish, but it’s the cheap version of the cheap phone for developing countries).

So… this “solution” might remind you of the Atari ST days!

Let the player decide whether he wants crashes or no music on his 520ST :slight_smile:
emscripten ports have access to hardware memory amounts, but not us HTML5 game devs.

N.B. Runs perfectly well with music (an no crashes) on the 1040ST… err, 512megs Kaios devices, I mean :wink:

im not sure if a window.reload in simple javascript will do, it doesnt seem to work always on linux/firefox (i dont test on other stuff much) as over time it still starts lagging but afaik “in theory” reloading the page should flush everything so instead of restarting scenes on a gameover if you reload the whole page (in theory or maybe that was somewhere in the past) it should all be flushed (caveats … lol) and the game starts from menu again (ofc that wont work mid-game unless you start making constructs with cookies etc…-) but at game-over or init or back to menu (in theory) i suppose it should

(someone do correct me if im wrong here) i sometimes leave it on and in the morning (at wake-uptime) i find it lagging while it shouldnt so there must be some buildup or leakage somewhere

Hi,

Interresting point - however for some reason reloading the page from the app’s standpoing (like we’d do via capacitor / cordova) just reloads a folder view, showing the list of files at the root of the web app’s www folder.

Reloading would indeed expose the user to the boot menu (“music / no music” in my case) even though the engine keeps track of the user’s progress every checkpoint / scene switch. I’ll stick to the “music on/off” boot option since 256megs devices are on the low end of the feature phone spectrum… which is already on the low end of specs, generally speaking :slight_smile:

1 Like

that’s certainly interesting :slight_smile: … i can only speak for webpages in mostly firefox and chrome

it just gets me the page its called on which is pulled from the server (i suppose) and parsed over again, including serverside php (or so i hope to think, im certainly more of a hobbyist :slight_smile:

thanks for the info on that

1 Like