memory issue

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

Moderators: Shine, Insert_witty_name

Post Reply
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

memory issue

Post by the underminer »

I've been working secretly on a 2D scrolling platform game inspired by splinter cell, of wich I will release a testing version soon. This may well be one of the largest luaplayer projects I know of. But, I've got a problem: memory. It runs out. I try to load as much as I can at the program's startup, so that I don't have to reload a lot. This involves loading roughly 60 png's and 3 small wav's. After all this is done, I start my level editor (yes,that's included too(-: ) wich gives me a memory indication: 6 megs. This gradually decreases when loading levels. Luckily, I've managed to let the fileselector load nothing when it's starting, wich would mean I would crash on saving a level in case of low mem.

But is loading everythin at startup the right approach? Or do you have other tips? Please let me know. I'm using 0.16, so it doesn't load modules or stuff like that (wich is good for mem usage)
Behold! The Underminer got hold of a PSP
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Try setting variables to nil when your done useing the data.

IE Loading Levels, Sprites, etc..
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

can't get it done

Post by the underminer »

Yes Romero, that's my plan, but I can't get it to work. look at this:

Code: Select all

--some other stuff
	elseif item == 3 then
		
		animations={Image.load("./animation/running_right.png"),5,Image.load("./animation/running_left.png"),5,Image.load("./animation/sam_crouchwalk_right.png"),8,Image.load("./animation/sam_crouchwalk_left.png"),8 --1 to 8
		,Image.load("./animation/sam_gunreach_right.png"),6,Image.load("./animation/sam_gunreach_left.png"),6,Image.load("./animation/sam_gunwalk_right.png"),8,Image.load("./animation/sam_gunwalk_left.png"),8 -- 9 to 16
		,Image.load("./animation/sam_elbow_right.png"),6,Image.load("./animation/sam_elbow_left.png"),6,Image.load("./animation/sam_shoot_right.png"),6,Image.load("./animation/sam_shoot_left.png"),6 -- 17 to 24
		,Image.load("./animation/ClimbOnBlock_right.png"),4,Image.load("./animation/ClimbOnBlock_left.png"),4,Image.load("./animation/ClimbOn2Block_right.png"),7,Image.load("./animation/ClimbOn2Block_left.png"),7 --25 to 32
		,Image.load("./animation/jump_right.png"),7,Image.load("./animation/jump_right.png"),7,Image.load("./animation/falling_right.png"),3,Image.load("./animation/falling_left.png"),3,Image.load("./animation/Ladder_up.png"),7 --33 to 42
		,Image.load("./animation/Jump_up_right.png"),3,Image.load("./animation/Jump_up_left.png"),3,Image.load("./animation/TurnToladder_right.png"),5,Image.load("./animation/TurnToladder_left.png"),5,Image.load("./animation/shimny_right.png"),7,Image.load("./animation/shimny_left.png"),7--43-54
		,Image.load("./animation/sam_diying_right.png"),3,Image.load("./animation/sam_diying_left.png"),3} -- 55-58
		
		BufferX,BufferY = Image.createEmpty(64, 320),Image.createEmpty(480, 64)
		DeBrief = Image.load("./animation/debriefing.png")
		Failed = Image.load("./animation/failed.png")
		StatsBar = Image.load("./animation/statsbar.png")
		bulletR,bulletL = Image.load("./animation/bulletR.png"),Image.load("./animation/bulletL.png")
		pangR,pangL = Image.load("./animation/pangR.png"),Image.load("./animation/pangL.png")
		Loaded[3] = 1
	end

Code: Select all

function Unload(item)
if item == 3 then
UnloadTable = {animations,BufferX,BufferY,DeBrief,Failed,StatsBar,bulletR,bulletL,pangR,pangL}
end
for i,var in ipairs(UnloadTable) do var = nil end
end
When I call my level editor with Unload(3) (all items in 3 are not neccesary for the editor) at the first line, it still says 2mb free. Almost worse than I started with... Pretty depressing right here...
Behold! The Underminer got hold of a PSP
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Try organizing your tables a little bit better. It seems like your tables.. should be setup simpler.

Code: Select all

function Unload(item) 
	if item == 3 then 
		UnloadTable = {animations,BufferX,BufferY,DeBrief,Failed,StatsBar,bulletR,bulletL,pangR,pangL} 
	end
	for i,var in ipairs(UnloadTable) do
		var = nil
	end 
end
Does not remove all instances of the table, Unless Unload(item) returns a new table which you use to write over the existing table. Thus redefining the table and clearing up the existing memory.

Your table configuration also seems a little bit on the weak side.
Try useing nested tables for animations. Then you can search for the object by ID, or use it in an array.

Example

Code: Select all

Animations = {
	{ "ID1", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID2", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID3", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID4", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID5", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID6", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID7", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
	{ "ID8", Image.load("./animation/sam_crouchwalk_right.png"), DisplayTime	},
}
There are some options for you to use. Please define all instances of the variable to nil and make shure there is no copys of the tables anywhere else.
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

Thanks for your quick reply.
when I'm having a problem that I can't solve after 2 days, I like to make some test scripts to clear my mind. Look at this:

Code: Select all

print(System.getFreeMemory() / 1024)
animations={Image.load("./animation/running_right.png"),5,Image.load("./animation/running_left.png"),5,Image.load("./animation/sam_crouchwalk_right.png"),8,Image.load("./animation/sam_crouchwalk_left.png"),8 --1 to 8
		,Image.load("./animation/sam_gunreach_right.png"),6,Image.load("./animation/sam_gunreach_left.png"),6,Image.load("./animation/sam_gunwalk_right.png"),8,Image.load("./animation/sam_gunwalk_left.png"),8 -- 9 to 16
		,Image.load("./animation/sam_elbow_right.png"),6,Image.load("./animation/sam_elbow_left.png"),6,Image.load("./animation/sam_shoot_right.png"),6,Image.load("./animation/sam_shoot_left.png"),6 -- 17 to 24
		,Image.load("./animation/ClimbOnBlock_right.png"),4,Image.load("./animation/ClimbOnBlock_left.png"),4,Image.load("./animation/ClimbOn2Block_right.png"),7,Image.load("./animation/ClimbOn2Block_left.png"),7 --25 to 32
		,Image.load("./animation/jump_right.png"),7,Image.load("./animation/jump_right.png"),7,Image.load("./animation/falling_right.png"),3,Image.load("./animation/falling_left.png"),3,Image.load("./animation/Ladder_up.png"),7 --33 to 42
		,Image.load("./animation/Jump_up_right.png"),3,Image.load("./animation/Jump_up_left.png"),3,Image.load("./animation/TurnToladder_right.png"),5,Image.load("./animation/TurnToladder_left.png"),5,Image.load("./animation/shimny_right.png"),7,Image.load("./animation/shimny_left.png"),7--43-54
		,Image.load("./animation/sam_diying_right.png"),3,Image.load("./animation/sam_diying_left.png"),3} -- 55-58
print(System.getFreeMemory() / 1024)
animations = nil
print(System.getFreeMemory() / 1024)
animations = {}
print(System.getFreeMemory() / 1024)	
The result is:
19456 -- the memory left after starting luaplayer
15360 -- memory left after loading the images, wich apparantly require 3mb
15360 -- memory available after setting animations to nil, no memory is cleaned
15360 --memory available after redifining table animations, overwriting it like you suggested. Doesn't help either

PS. This script stands alone and thus I can be sure there are no references to it.
PS 2:

Code: Select all

for i,item in ipairs(animations) do
	item = nil
end
print(System.getFreeMemory() / 1024)
gives the same result
Behold! The Underminer got hold of a PSP
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Try adding a call to the garbage collection function, I think this is right but I don't use Lua, sorry:

Code: Select all

collectgarbage()
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

that command exists in lua, but I had no results again...
Behold! The Underminer got hold of a PSP
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

the underminer wrote:that command exists in lua, but I had no results again...
There was a problem in earlier version of Lua Player. With 0.20 and this code, saved as script.lua:

Code: Select all

white = Color.new(255, 255, 255)
screen:print(0, 0, System.getFreeMemory() / 1024, white)
test1 = Image.load("Applications/Snake/desert.png") 
test2 = Image.load("Applications/Snake/desert.png") 
test3 = Image.load("Applications/Snake/desert.png") 
screen:print(0, 8, System.getFreeMemory() / 1024, white)
test1 = nil 
test2 = nil 
test3 = nil 
collectgarbage() 
screen:print(0, 16, System.getFreeMemory() / 1024, white)
while not Controls.read():start() do
	screen.waitVblankStart()
	screen.flip()
end
I get the result 9216, 6144, 9216.

So the right solution would be first to fix the memory problem with loading modules (maybe Oobles has any ideas about it) and then the modules loader for firmware > 1.5.
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

Thanks for your reply Shine, but the big downside to this good news is that 020 has a whooping 10mb less memory. Still I tried, but when loading my first level I got this can't create image error (Because it ran outta memory)

I'll just think about what to do next. Maybe I can alter my code so that it can cope with 9 megs, but I don't think Ill manage to do so.

Thanks again
Behold! The Underminer got hold of a PSP
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

the underminer wrote:Thanks for your reply Shine, but the big downside to this good news is that 020 has a whooping 10mb less memory.
Yes, that's what I mean with "the memory problem". Oobles, who implemented the module support, wrote me an eMail that it could be a limitation of the PSPSDK, because you have to specifiy how much memory each module needs at compile time. But currently I don't have time to fix it, maybe ask Oobles.
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

I asked Oobles, but he didn't respond. Maybe someone else can do this for me? I would really appreciate it.
Behold! The Underminer got hold of a PSP
cools
Posts: 46
Joined: Sat Mar 04, 2006 12:57 pm

Post by cools »

Well if you wanted a non-module, lots of memory capable lua player check out either mine or youresam's mods to it. They both give around 20 megs or so.
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

thats the mp3 and ogg supported mod right cools?

BTW You left out the over/undeclocking because is is dangerous you say. Is it dangerous when you use the luacode for it or dangerous in itself? I ask because I always use IRShell to overclock LUA as 0.16 cant overclock itself as you know.
cools
Posts: 46
Joined: Sat Mar 04, 2006 12:57 pm

Post by cools »

Yup the Ogg/Mp3 one is non-module. It was originally intended to be used with the original 2.00 tiff exploit (since that is what i was using) with luaplayer .20, since at the time you could only use luaplayer .16 on 2.00.

The Over/Underclocking was removed; The function(s) itself were not harmful to the psp in any way, its just that if one user decides to:

Code: Select all

function overloadpsp()
l = 1
System.setClockSpeed(l)
l = l + 1
end
Even calling any one of the system over/under clocking functions in a while true do loop could be harmful. (Setting your psp to 333 mhz every loop... not good)

Stick with IRShell, its a lot safer!
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Allright nice! And you almost bricked your own PSP with that? Haha almost fell in your own trap. Anyway thx for your mod. Finally mp3 and ogg support. Makes life alot easier. Is there any limit on file size or is that just limited by the amount of free memory?
Mechanical
Posts: 4
Joined: Mon Feb 06, 2006 9:13 am

Post by Mechanical »

Hi ;)
I have a noob question. How to unload png from memory?

screen = Image.load("screen.png")
screen = nil

??
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

Mechanical wrote:Hi ;)
I have a noob question. How to unload png from memory?

screen = Image.load("screen.png")
screen = nil

??
You can never use screen to store an image in!!
screen is reserved for what you see on the screen. To show an image on the screen you do this:

Duck = Image.load("duck.png")
screen:clear() -- removes everything that was on the screen buffer
screen:blit(0,0,Duck)
screen.flip() -- this draws on the physical screen what is in the screen buffer. Anything you change in the buffer'screen' will only be visible after you do screen.flip()
Behold! The Underminer got hold of a PSP
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

and yes to unload it you would do "Duck = nil"
Mechanical
Posts: 4
Joined: Mon Feb 06, 2006 9:13 am

Post by Mechanical »

big thanks
ducks - new playstation symbol ;))

ok, another question...
i have not any idea why i can't use all psp memory... i can't load more than ~ 3.6MB :/
(luaplayer 0.20).
it is possible to load more data to memory?
thx.
Post Reply