Efficient File Loading / Memory Usage Tips?

Hello there! I’ve developed quite a large game with Processing, and I’m wanting to know the best way to load a large number of files (200+). I’ve tried loading them all with a thread called in setup(), but I’m concerned that loading them all at once and keeping them loaded might impact the game. I’ve also tried loading only a few at a time by having a loadLevel() function and setting some Image and Sound variables equal to loadImage() and loadSound() outputs based on the level being loaded, however, this would cause the program to stutter more and more each time something was loaded, until eventually the game was unplayable.

So really, I have no idea how to properly handle memory with a high volume of assets is in play, and it’s taking a toll on my game for some of its users. The only help I could find elsewhere was “load it all at the start”, and although the thread() method works a bit, the game is still visibly jumpy and stuttery at times, and for some people the game refuses to load altogether. What’s some general advice for memory management? Is there a way to load and unload files such that not much memory is being used? Is there some sort of cache I can clear out to make sure the game doesn’t get all gunked up with excess data? Although my game has a LOT of assets, it usually doesn’t need more than 20 or so at a time, so I would hope there are realtively simple methods of making sure the game is only using what it needs when it needs it without the game crashing or getting laggier and laggier each time it reloads something. Thank you in advance!

1 Like

Hi,

Common guidance is to load everything at setup, so that draw-cycle can then be run without disruptions. And it can then be guarantied that all resources have really been loaded, before draw-cycle starts.
In your case it might not work.
I can see couple of options.

  1. Can you combine your resources to have less files? It might help in loading them. Also reducing size of the resources might partly solve the problem.
  2. Make a load routine that stops game between scenes, if you have fitting changes or pauses in game play.
  3. If you know before hand what resources you need, it could be possible to load them in the background. It most likely has an effect to frame rate at draw-cycle.
  4. If memory is really the constraint and you are using Java, you can tune processing start command to increase available memory.

Releasing memory is up to garbage collection and there isn’t much you can do about it but remove all references to object and wait it happens.

1 Like

Thank you for the response! I tried doing a load routine between levels before, but that gradually made the program stutter more and more, even if I wasn’t loading that many files each time. Would this be because Processing tries to keep everything loaded since setup() in memory? Also: I looked a little further into the depths of the Processing Forums and found an undocumented “dispose()” method… do you think that could be used to fix this issue?

Previous discussion of dispose.

https://forum.processing.org/two/discussion/19009/dispose-method-in-glvideo

Not sure what method you are using to load files. But what I tend to do is to cache the files as string locations, which is done through listFiles() that way you load what you want by grabbing the link to the file.

Also try and see if there are any instances of you creating new objects and see if this can be assigned to a singular class variable instead.

If you have a lot of images in a game, the best thing may be to look at creating a sprite sheet. One or more large images that contain all the smaller images that are used together. You then need to know how to reference each individual image by its coordinates in the larger image. You can use the (somewhat undocumented) version of image(..) that takes nine arguments, or use texture coordinates.

You could even create something like a Sprite class to simplify things.


class Sprite {
  PImage sheet;
  int x, y, w, h;

  Sprite(PImage sheet, int x, int y, int w, int h) {
    // set fields here
  }

  void draw(int xPos, int yPos) {
    image(sheet, xPos, yPos, w, h, x, y, x + w, y + h);
  }

}