When do module scripts release their cached result?

Hi, everyone!

Recently, I was prompted a question regarding module scripts, and it got me thinking: ‘when do module scripts actually release their cached output?’

For context, module scripts only run once, right? And after the first time they’re requested (per client or server), they just return the same result.

But where exactly is this result stored? In the module script instance itself? In the script that first requested it? Somewhere else entirely?

Generally, when (if ever) can I be sure that this cached output will no longer be in memory?

1 Like

When dealing with memory, unfortunately for Roblox that’s all in the C side code which handles memory for stuff like garbage collection I believe.

Same goes with where the memory is located and how Roblox internally implements the require function.

Without access to the C code one can only take wild guesses as to where the memory is stored specifically.

Might want to ask Roblox staff instead.

Otherwise I believe the garbage collection rules should apply, it’ll be in memory until there is no longer a reference to it.

local module = require(stuff)

wait(99999)

print(module)
module.doStuff()-- still in reference
--and thread is still alive
1 Like

I suppose me asking ‘when will it no longer be in memory’ was a bit vague, but I was trying not to abstract the question. I’m really asking when I can count on it being swept up by garbage collection.

The issue, is that since I don’t know where this reference is kept, I don’t know when the module’s cache will actually be considered unreachable. Perhaps when there’s no references to the module script itself? I’d certainly hope so.

I’ll do some testing around in order to get something more definitive. Thanks for giving it a shot anyway. (If anyone knows for sure in the meantime, feel free to say as much lol)

As far as I know, the cached result from a module script is per-module, Roblox will save this return in the loaded global state, which makes it permament until the GC is allowed to purge it.

If the ModuleScript (or any references to the object it returns) remains in the game/memory, it will remain there as this is generally how the GC works.

From an outsider looking in, without being able to control memory. No, you cant release a ModuleScript’s cached result unless it’s been destroyed, and everything has dropped it’s returned value (if it’s a table or function).

3 Likes