How to release the textures of a closed document #651
-
Hello, First, thank you for your great library! Second, I have a question: I'm using release v5.1. When closing a document, I expect the textures to be released, but they still live in the internal texture database after close. Is it intended and is there a way to release them? Currently I'm using |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 3 replies
-
I was actually going to ask a similar question; I had thought that maybe images were ref-counted/garbage collected in some way, but thinking more about it I assume it doesn't do that because most UI systems don't have "temporary" documents and keep most of their images around forever. My first thought was that maybe you could mark clearable textures with a query argument in the URI or something (ie, |
Beta Was this translation helpful? Give feedback.
-
Hello, and thanks for the kind words! You're right, we don't release textures automatically until the library is shut down. The idea is that many use cases involve showing and hiding elements with images, or rewriting the RML with images involved. Or even successively opening and closing documents. In such cases, the assumption is that one normally want to keep textures around so that they don't need to be reloaded unnecessarily. I understand this behavior doesn't fit all use cases, and indeed, calling We actually used to have reference counted textures through shared pointers (in RmlUi 5.1), but even then we never actually released them for the above-mentioned reasons. So during the rewrite of the texture database for RmlUi 6.0, I got rid of the reference counting all-together. Maybe it would be nice to re-introduce reference counting, and at least give that ability to garbage collect/release only textures that are unused. Since texture resources are shared between documents, I don't think marking individual textures for garbage collection at the document level is the right approach. But perhaps having a manual garbage collection as outlined above would be sufficient for most scenarios? |
Beta Was this translation helpful? Give feedback.
-
Hello, Thank you for your detailed answer.
Granularity in textures life time would be a matter for embedded users mostly, as in my case. I didn't come up with a specific idea, but generally speaking, a possible approach would be to let users somehow manage the life time. This way library doesn't have to worry about it and users will have more flexibility. And of course, with flexibility comes a bit of complexity for users. |
Beta Was this translation helpful? Give feedback.
-
Agreed, we definitely need something like this. One use-case we have is that we tend to do a "vault" of sorts with games where we put in unused content; we don't want this content to stay alive outside of the vault UI and would prefer to just eliminate it once you back out of the Vault (and we precache it when you're entering the vault so you don't experience hitches when exploring it). This is very important for consoles especially; a lot of the content you browse would be images, and the Switch doesn't have a ton of GPU memory to spare, heh. I don't think it's a good idea to do this at a global level, though, because there's still a lot of textures loaded that we do want to keep alive, even if the documents aren't open. You don't want hitches coming back when loading other submenus (although if we keep the images cached in our own texture manager, we can avoid them I guess). |
Beta Was this translation helpful? Give feedback.
-
Hm, right, I can fully understand this need. Perhaps some global function that takes a source name (or perhaps a TextureHandle) and tells the library to automatically garbage collect it. Could that be helpful? Although, we already have the ability to release individual textures that way, but it's perhaps not always so easy to know when it's closed. |
Beta Was this translation helpful? Give feedback.
-
Yeah. I overlooked the new global function That does the job for me, though in my use case, a document level function like this would be easier to use and to avoid hardcoding texture names which is a long list: |
Beta Was this translation helpful? Give feedback.
-
As I'm looking,
That can also be used on C++ side to find objects which is very useful. For example:
In Qt all classes are derived from |
Beta Was this translation helpful? Give feedback.
Hello, and thanks for the kind words!
You're right, we don't release textures automatically until the library is shut down. The idea is that many use cases involve showing and hiding elements with images, or rewriting the RML with images involved. Or even successively opening and closing documents. In such cases, the assumption is that one normally want to keep textures around so that they don't need to be reloaded unnecessarily.
I understand this behavior doesn't fit all use cases, and indeed, calling
Rml::ReleaseTextures()
is the intended way to release textures in such scenarios. So I would suggest to continue using this in your case. One perhaps unfortunate effect of this is that it r…