Best way to handle multiple references to a resource

Hey guys,

If I have a “Content” class which stores a static sprite image, and then I create 100 sprites which all take this image as a parameter, will I have 100 instances of this image in RAM or just 100 pointers to the static image?

This question has come about from a TileMap engine that I am writing and whether it is safe for each Tile object to keep hold its image or if it should maybe just keep a row and column parameter which indicates the tiles position on a sprite sheet.

You’ll have 100 ‘pointers’ (references, generally speaking) to a single instance. That’s assuming this is Java you’re talking about, and that there’s no cloning or deep copying going on that you haven’t mentioned.

Thank you for the quick reply!

I am using Java and just to clarify further, I have a Content class which I use at the start of my game to store each of my images as a final static variable, then when I create an entity I will set its image as below.


public entity() {
    this.image = Content.entityImage;
}

I hope / don’t imagine that there would be any copying that I am unaware of using this method.

I have seen people using a hash map as a way to manage their content which I will look into later if it is a better option.

Hi

Do you know the design pattern “Flyweight”?

[quote=“unlight,post:1,topic:55224”]
The best/only way to answer questions like this is to do some profiling. How much memory does your program use if you have a single reference to the image? How much memory does it use if you have 100? 1000? 1,000,000?

But to understand the basics of what’s going on, I highly recommend reading these two articles:

http://www.javaranch.com/campfire/StoryCups.jsp

http://www.javaranch.com/campfire/StoryPassBy.jsp

Use a profiler to understand by-reference? Surely by the time you know enough to make sense of the profiler output, you will probably understand by-reference… :slight_smile:

No I haven’t, I will read into it now.

[quote=“KevinWorkman,post:5,topic:55224”]

Thank you for the links, I love this guys way of explaining things, big help!

His original question was about how his code affects memory. The way to test that is by using a profiler.

But to really understand what’s going on under the hood, then understanding what “pass by value” means with references is a good place to start.

Hi

The articles quoted by KevinWorkman seems to be easy to understand.

Don’t load everything at startup or the bigger your program is, the longer it takes to start. It’s ok if it remains tiny. Otherwise, you should load an image only when you really need it and you should keep it during a certain amount of time in order to avoid reloading it each time except when you risk to run out of memory. You can split the whole life of your software from the start to the end into cycles, maybe you don’t need everything always. For example, as we are on Java-Gaming.org, when you enter the level 11, you don’t need to keep the sprites of the bad guys located in the level 1, do you? Another strategy consists in using a kind of reference able to “drop” the referenced object(s) when you risk to run out of Java memory, you can look at weak references (but keep in mind that WeakHashMap uses weak keys, not weak values, i.e it’s a bad candidate for caching).

It’s important to know how to identify a resource, in order to retrieve it further. You can use the path of the image file to identify an image, it’s more flexible than creating a static field in a class each time you want to use a new image but you can still do it to store the relative path of the entity image.

Maybe the advises above are a bit too much for your use case, it’s up to you. Good luck.