Decrease your memory footprint by using resource wrapper
The JRebirth Framework provides an useful way to deal with your local resources, we currently support :
These resources can consume a lot of memory if you don’t dispose them (especially big images and fonts) when you stop using them. JRebirth provides a mechanism to store them weakly and to rebuild them if necessary in order to use the less memory as required. To do this JRebirth stores a ResourceParam lightweight object that contains all information to create the heavyweight resource. This one is linked by a ResourceItem to facilitate its usage.
UML Diagram Overview:
Heavyweight resource object can be retrieved by calling the get() method of ResourceItem. If the resource was already loaded it is retrieved from cache otherwise it’s loaded another time and stored weakly. When system goes low memory, all resrouces not strictly retained by a Component or a Scene’s node will elligible for garbage collection to release some bits.
There are 3 ways to declare resources, each one is available for all kind of resources with special feature for parameters.
The first way is to hold static field declaration instantiated with custom ‘factory’ Resources (not named, with an overloaded create method to avoid later cast). This static fields can be hold in any class you want but we recommend to store them into an Interface used ‘as’ an enum. It’s better to have an interface per resource type to avoid big file that blends all resources. Another interesting trick, is to add a static import on related static Resource.create method to shorten the resource declaration (Ctrl+Shift+M with Eclipse).
Static import declaration:
Example of Web color declaration:
The other way to declare is a little bit complex, it implies to create an enum that implements a ResourceItem interface. But it requires to add use a custom syntax to use default method.
Severals default methods exist, at least one per ResourceParams to create.
We can load dynamicalli resource by creating “anonymous” ResourceItem, the resource will be kept within cache if memory is not constrained otherwise it will be deleted. Another call with the same path will hit the cache and the image will be only stored once, but created as many times as deleted and retrieved again.
In this sample item.getImage() return the relative srtring path of the image.
For example to manage web color, (basic hexadecimal string #00CC00), you have to use this declaration:
For example to manage the Turtles font, you have to use this declaration:
Hereafter the enumeration that stores font names.