how does memory work with lua

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

how does memory work with lua

Post by my psprecious »

If I have a script that calls another script, does the first script get erase from the memory or is it still running, if it's still running can I stop it from running and keep some variables in memory (do I need to write them to a file before)

Hope it's clear, I'm new at lua since last friday, and I'm not a coder, I'm a gfx artist, but lua is really fun to learned.

If it's not clear just let me know I'll try to explain differently and the best I can

thanx all for your help
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

LUA is call based. If you run another script.. the previous script is still loaded in active memory.
fullerlee
Posts: 54
Joined: Thu Nov 03, 2005 9:46 am

Post by fullerlee »

This seems like a good place to ask about Garbage Collection in luaplayer.

- Are locals marked for GC as soon as they go out of scope?
- Will globals be marked for GC if we nil all references?
- When does GC run (every n secs, when mem usage reaches a threshold etc) ?

Lee
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

romero126 wrote:LUA is call based. If you run another script.. the previous script is still loaded in active memory.
is there away to clear them from the memory, here's an example

I call a level for a game it call images and other stuff that are only used for that level, so when I get to another level is there a way to clear the previous one from memory?
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

romero126 wrote:Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format.
Big thanx romero126

so if I understand this right if I called a png that takes lots of memory and then make it = nil it wil be out of the memory

thanx again, sorry if I sound a bit ignorant, just started lua last week and considering my knoledge of programming = nil, well Lua is super easy especially with help from people like you

Just tought of something, since the lowser is a script that start a script everytime we come out of a lua application or game and go back to the lowser the image of previous script stay in the memory (corect me if I'm wrong I'm just trying to understand) so is there a way to clear all on exit, or am I wrong and it does it. I ask this cause I was fooling around with the controls script tutorial from wiki.ps2dev.org and it happen so I wanted to be sure.
indianajonesilm
Posts: 15
Joined: Tue Feb 08, 2005 6:36 pm

Post by indianajonesilm »

Check out this old post by Shine where he explains about how he fixed the automatic Garbage Collection in luaplayer:
http://forums.ps2dev.org/viewtopic.php? ... 7341#24006

Also a Garbage Collection Tutorial can be found here:
http://lua-users.org/wiki/GarbageCollectionTutorial
Which explains the collectgarbage() function.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

romero126 wrote:Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format.
That's wrong. Take a look at this code:

Code: Select all

foo=Image.load("test.png")
bar=foo
foo=nil
In bar is the same image object reference stored as in foo. When setting foo to nil, there is still a reference to this object in bar, so the image will not garbage collected. But when you set bar to nil, too, then the image will be collected.

Note that "foo" and "bar" in this example are global variables. When using local variables, it will be garbage collected when out of scope, but not in this example:

Code: Select all

function test()
  local foo = Image.load("test.png")
  bar = foo
end
because bar is a global variable and the object reference for the image is stored to it, so after leaving the function scope, the image is still accessible with bar.

To answer the original question: If you call a script for every level, it is a good idea to use the same global variables for each level, e.g. "images={start=Image.load("start.png"), foo=Image.load("foo.png")}". Then the next level creates a new table for the images variable and the table for the first level and all images are not accessible any more (if not stored in another global variable) and they will be garbage collected.

And: If you call another script, the caller continues after the call instruction after the called scripted ends. You can call a script with dofile. But it is a good idea just to define some level variables and perhaps some special level functions and implement the main game loop in only one file.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

Wow that definetly answers my question, thanx shine.

So you propose I do everything in one file instead of different files, now this brings another question , is there a file size limit
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

my psprecious wrote:So you propose I do everything in one file instead of different files, now this brings another question , is there a file size limit
There is no limit for the script file, only available memory. But you can use different files, e.g. one for each level, but I meant that you should not duplicate code, e.g. the function for checking the controls, but write these functions all in one file (or in more files, one file for each functionality, if it is large, because then it is easier for you to reuse it for other games). For example:

your-game-library.lua:
- general purpose side-scroller functions
- general purpose level handling functions

index.lua:
- main game loop with controller functions
- level loading algorithm

level1.lua:
- defines a level variable, e.g. with callback to level specific functions

level2.lua:
...

But I would start with one file and if you think it is getting to complex, then split it into multiple files.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

Shine I love your explanation, very clear and simple

you just made me understand exactly what function where for (sorry for my ignorance, started lua a week ago and for only background in programation basic from 20 years ago)
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Shine wrote:
romero126 wrote:Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format.
That's wrong. Take a look at this code:

Code: Select all

foo=Image.load("test.png")
bar=foo
foo=nil
In bar is the same image object reference stored as in foo. When setting foo to nil, there is still a reference to this object in bar, so the image will not garbage collected. But when you set bar to nil, too, then the image will be collected.

Note that "foo" and "bar" in this example are global variables. When using local variables, it will be garbage collected when out of scope, but not in this example:

Code: Select all

function test()
  local foo = Image.load("test.png")
  bar = foo
end
because bar is a global variable and the object reference for the image is stored to it, so after leaving the function scope, the image is still accessible with bar.

To answer the original question: If you call a script for every level, it is a good idea to use the same global variables for each level, e.g. "images={start=Image.load("start.png"), foo=Image.load("foo.png")}". Then the next level creates a new table for the images variable and the table for the first level and all images are not accessible any more (if not stored in another global variable) and they will be garbage collected.

And: If you call another script, the caller continues after the call instruction after the called scripted ends. You can call a script with dofile. But it is a good idea just to define some level variables and perhaps some special level functions and implement the main game loop in only one file.
Please make code hideously more complicated in explination as it needs to be. Understanding that all variables are just pointers to a set of information makes what you said make absolute sense but at the same time. To a person new to the LUA codeing which possibly means new to programming in general makes what you said hideously complex.

Removing complexitys is needed when talking to people curious to know about data collection and variable pointer paths.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

actually I am new to programming, but 22 years ago I use to program basic on ZX-81 and commodore 64

but I'm starting to understand again, still a few things I need to understand, but thanx to shine earlier I understand function
Post Reply