[Resolved] Dose require() consume Memory?

I have a weird question:

Dose require() other files consume Memory or CPU?
If so, is it a lot? And after require() other files, how to cancel it to release the memory after I am down to use the function in other files? (Is there a similar API like object:removeSelf() for require()?)

I was thinking about require() my 10 other files in one scene. (I am using storyborad).

Thanks
[import]uid: 81853 topic_id: 33478 reply_id: 333478[/import]

Yes. The code behind the function itself take a small amount of memory, and loading in libraries and code takes up memory.

For unrequire and other techniques, here is a good read:
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
PS - “Yes,” also to CPU and how much depends on what you are requiring(). For an example, SSKCorona takes up about 822KB, but then it has a lot of stuff in it (and can be trimmed down if you only need certain parts.) [import]uid: 110228 topic_id: 33478 reply_id: 133030[/import]

Hi Dan,
“require()” basically adds an external module to Lua’s “package.loaded” table. So yes, it effectively consumes memory: essentially the amount “required” by the external module, plus perhaps a tiny bit more.

To completely clear out a required module, you need to access the ID of that module within “package.loaded” and “nil” it out, as in

package.loaded[module] = nil

Then the Lua garbage collector can free it up from memory.

Here’s a slightly more technical explanation… and I’m sure there are some truly DEEP technical explanations out there if you search for them. :slight_smile:
http://www.lua.org/manual/5.1/manual.html#5.3

Brent
[import]uid: 200026 topic_id: 33478 reply_id: 133031[/import]

In my experience files loaded with require() can be a major source of memory leaks when you switch from scene to scene. Corona caches files you require and does not garbage collect them or the variables defined within them automatically.

I had a case where a huge amount of memory was being leaked by animated sprites loaded in a required() file, where the memory would not be cleared even though the objects were placed in the scene’s displayGroup and that displayGroup was removed. However, I’m not sure if that was due to a 3rd party library I used or something to do with Corona itself.

In either case, I highly recommend using the unrequire() method mentioned above once you’re done using each required file (Typically at the end of a scene). [import]uid: 135827 topic_id: 33478 reply_id: 133045[/import]

@george18 and @emaurina added some excellent points… thank you both. :slight_smile:

So, @dan300, I think you can determine the best method based on these remarks. I would just add one last piece of advice: don’t OVER-require. Using modules is a useful and often necessary technique, but it doesn’t make sense to load a Storyboard scene and then require 10 additional modules within it. That’s just opening the gates for massive memory leaks if you forget to or fail to clean up everything immaculately. And it doesn’t make good design sense either. So, use modules but use them wisely.

Brent
[import]uid: 200026 topic_id: 33478 reply_id: 133150[/import]

Yes. The code behind the function itself take a small amount of memory, and loading in libraries and code takes up memory.

For unrequire and other techniques, here is a good read:
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
PS - “Yes,” also to CPU and how much depends on what you are requiring(). For an example, SSKCorona takes up about 822KB, but then it has a lot of stuff in it (and can be trimmed down if you only need certain parts.) [import]uid: 110228 topic_id: 33478 reply_id: 133030[/import]

Hi Dan,
“require()” basically adds an external module to Lua’s “package.loaded” table. So yes, it effectively consumes memory: essentially the amount “required” by the external module, plus perhaps a tiny bit more.

To completely clear out a required module, you need to access the ID of that module within “package.loaded” and “nil” it out, as in

package.loaded[module] = nil

Then the Lua garbage collector can free it up from memory.

Here’s a slightly more technical explanation… and I’m sure there are some truly DEEP technical explanations out there if you search for them. :slight_smile:
http://www.lua.org/manual/5.1/manual.html#5.3

Brent
[import]uid: 200026 topic_id: 33478 reply_id: 133031[/import]

In my experience files loaded with require() can be a major source of memory leaks when you switch from scene to scene. Corona caches files you require and does not garbage collect them or the variables defined within them automatically.

I had a case where a huge amount of memory was being leaked by animated sprites loaded in a required() file, where the memory would not be cleared even though the objects were placed in the scene’s displayGroup and that displayGroup was removed. However, I’m not sure if that was due to a 3rd party library I used or something to do with Corona itself.

In either case, I highly recommend using the unrequire() method mentioned above once you’re done using each required file (Typically at the end of a scene). [import]uid: 135827 topic_id: 33478 reply_id: 133045[/import]

@george18 and @emaurina added some excellent points… thank you both. :slight_smile:

So, @dan300, I think you can determine the best method based on these remarks. I would just add one last piece of advice: don’t OVER-require. Using modules is a useful and often necessary technique, but it doesn’t make sense to load a Storyboard scene and then require 10 additional modules within it. That’s just opening the gates for massive memory leaks if you forget to or fail to clean up everything immaculately. And it doesn’t make good design sense either. So, use modules but use them wisely.

Brent
[import]uid: 200026 topic_id: 33478 reply_id: 133150[/import]

@emaurina and Brent:
Thanks, really helpful explanation and links. Now I have better understanding about require().

@george18
“major source of memory leaks”? Sounds scary, now I got one more reason to not over do it.
Thanks all. [import]uid: 81853 topic_id: 33478 reply_id: 134108[/import]

So important information which most of the MEMORY LEAKS articles never covered before.

Thanks to all… [import]uid: 108129 topic_id: 33478 reply_id: 134124[/import]

@emaurina and Brent:
Thanks, really helpful explanation and links. Now I have better understanding about require().

@george18
“major source of memory leaks”? Sounds scary, now I got one more reason to not over do it.
Thanks all. [import]uid: 81853 topic_id: 33478 reply_id: 134108[/import]

So important information which most of the MEMORY LEAKS articles never covered before.

Thanks to all… [import]uid: 108129 topic_id: 33478 reply_id: 134124[/import]