Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Delete sprite and image

Author
Message
7RS
11
Years of Service
User Offline
Joined: 10th May 2012
Location:
Posted: 31st Jul 2012 12:07 Edited at: 31st Jul 2012 12:08
Hi,
when i create a sprite i can do 2 different thing:

1st:

2nd:


now i would like to know in the 1st case if is always necessary to delete the image when the sprite is deleted or it will be automatically deleted when the sprite will be deleted.

In the 2nd case i have the image so i can easy delete it but, in the 1st i don't have the image to delete.

Thanks.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 31st Jul 2012 12:26
Quote: "in the 1st i don't have the image to delete."

I believe the GetSpriteImageId command well help out in the first case.

I don't believe the image is automatically deleted when the sprite is deleted but I think AppGameKit automatically deletes images and sprites upon exiting the application. Don't quote me on that though.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st Jul 2012 16:21
If you use spriteID = loadSprite("myImage.png") this will automatically load the image and delete the image if the sprite is deleted.


this.mess = abs(sin(times#))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 31st Jul 2012 16:50
Quote: "If you use spriteID = loadSprite("myImage.png") this will automatically load the image and delete the image if the sprite is deleted."


Really? I didn't know that. A good bit of information.

@7RS, however, if you think that you are going to be recreating the same sprite with the same issue more than once (after deleting it of course), then you are better off with the 2nd bit of code you showed. Move the loadimage to somewhere where it happens once and then it is available whenever you need to create the sprite.

The loadimage/deleteimage functions take up more memory and time than the createsprite/deletesprite. The image stuff actually accesses the image file, doing a 'physical' device thing. The sprite bits are pure memory.

Cheers,
Ancient Lady
AGK Community Tester
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st Jul 2012 16:53
Quote: "The loadimage/deleteimage functions take up more memory and time than the createsprite/deletesprite. The image stuff actually accesses the image file, doing a 'physical' device thing. The sprite bits are pure memory."

Yes, that's right. I would only use "loadSprite" if I was going to keep the sprite for a while, for example as a background image that would maybe only be deleted after a game had finished.

It can save you from remembering to delete a lot of images that might be taking up precious memory. Also it can prevent you from loading the same image multiple times as it will only load the image if it is not already loaded...


this.mess = abs(sin(times#))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 31st Jul 2012 17:10
Hey baxslash, shouldn't your status be changed to something like 'Super AppGameKit Staff'?

Cheers,
Ancient Lady
AGK Community Tester
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st Jul 2012 17:13
They're adding me as a mod at some point I'm not in a rush as I'm snowed under, despite having time for a few looks at the forum today. I've been trying to keep up with activity though and decided to look at a few posts in more detail today.


this.mess = abs(sin(times#))
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Aug 2012 13:42
Quote: "They're adding me as a mod at some point "

I've thought of a title for you as well: "The Game Creator's Game Creator"

See what I did there?

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Aug 2012 14:10
Nice but I think my official title is "AGK App Developer"


this.mess = abs(sin(times#))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Aug 2012 16:56
Quote: "Nice but I think my official title is "AGK App Developer""

And a good title it is, too.

Cheers,
Ancient Lady
AGK Community Tester
7RS
11
Years of Service
User Offline
Joined: 10th May 2012
Location:
Posted: 2nd Aug 2012 16:15
Ok, considering i delete all my sprites when a level is completed, due to our level manager, would be good to use the LoadSprite.

In the online documentation i haven't found it, where is ? It's should be in the sprite section but it isn't.

Anyway i would like to know if using the loadsprite after the animation can be managed in the same way as before using the AddSpriteAnimation or SetSpriteAnimation.

Thanks.
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Aug 2012 16:39
Why do you guys keep loading and deleting things all the time, I just load what I need in the start of the game. Most phones has 512MB or 1GB of memory, and your games are like 4mb, and maybe a few 10's of mb in data to go. Whats the problem?

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Aug 2012 16:50
It makes sense that the more images being held in ram reduce the amount of ram remaining to run the game. I would assume that on less powerful devices this can make a lot of difference in speed.

I'm not sure how much of a difference it makes (I'm no expert) but when my game is running a little slow I'll try just about anything to eek a little more out of it!


this.mess = abs(sin(times#))
7RS
11
Years of Service
User Offline
Joined: 10th May 2012
Location:
Posted: 2nd Aug 2012 16:50
If you have 100 levels and you load the sprite everytime you will have a lot of memory leak.

My game has a lot of levels and on each level i haven't the same enemies or objects so i need to remove the old also because if you do not delete the unnecessary sprites in some devices you will have some performance problems.
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Aug 2012 16:55
Having alot of stuff in memory, should not affect the speed of the program. The ram is only accesses when needed, so no matter how much you fill it up, it should not affect speed. A device does not have a slow harddrive to use as virtual memory like a PC, so I don't buy it until someone tries it and proves me wrong

Now, running alot of programs in the background, will kill the speed of your program, maybe we should add a warning in the games, to close all programs, if they experience slowdowns when playing this game?

I would imagine deleting and recreating objects all the time will be a bigger slowdown factor in any game, then just leave them in there all the time.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
7RS
11
Years of Service
User Offline
Joined: 10th May 2012
Location:
Posted: 2nd Aug 2012 17:02
Ok try to create 100 sprites and after increase the size of them in terms of amount, check the framerate and you will see with 3000 sprites if the speed will be same. I do not speak about different tasks so many different apps running in background i mean in a single application if you do not delete your resources and sprite but instead you create a new one everytime you will have performance problems for sure.

Anyway you can write your own code, i'm not in the position to teach anyone how to program. You have your idea and knowledge and i have mine. I prefer to remove unused objects and to recreate them when is necessary.

Our old framework load a complete level and create all sprites at the beginning now, we changed it, we load and create all dynamically and seem to work pretty well, i prefer to remove what i don't need like this the loop of agk to check all available sprites is shorter and make me gain framerate.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Aug 2012 17:04
Quote: "The ram is only accesses when needed, so no matter how much you fill it up, it should not affect speed."

I think the key phrase here is "when needed". If the amount of ram needed is filled up how can it then be used?


this.mess = abs(sin(times#))
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Aug 2012 21:28
It's filled up with what you need baxslash. I don't say you need 3000 sprites, no, you only use what sprites you need on the screen, but no need to delete the images in memory, just reuse the sprites for each level, and assign new images to them.

If you have an enormous amount of images and sprites, this might be a problem yes, but I think 90% of all agk games can just load all resources at the start without getting a speed issue. if it's not rendered, it should not cause slowdown, or am I wrong?

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Aug 2012 22:23
Impetus73 sounds like he has it right.

It's mostly the images that take up memory, sprites don't take much at all.

Cheers,
Ancient Lady
AGK Community Tester
7RS
11
Years of Service
User Offline
Joined: 10th May 2012
Location:
Posted: 2nd Aug 2012 22:47
Anyway anyone can write their own code, as i said our game is huge and use a lot of resources we need to delete sprites and gfx when are not necessary.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 00:15
Sprites yes, but you can save a decent amount of memory only loading menu images when you're in your menu. I sped my current game up significantly that way. Not in theory.


this.mess = abs(sin(times#))
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Aug 2012 02:13
Quote: "Why do you guys keep loading and deleting things all the time, I just load what I need in the start of the game. Most phones has 512MB or 1GB of memory, and your games are like 4mb, and maybe a few 10's of mb in data to go. Whats the problem?"

I personally think that it's good practice to take up the least amount of memory possible.

Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 09:29 Edited at: 3rd Aug 2012 09:31
Hodgey, when you eat, do you try to use as little of the area of the plate as possible, to put your food on, keep moving and fetching new food from the fridge, or do you use the whole plate, and put all you need on it from the beginning? I feel the same with memory, there just is so much, it's meaningless to worry about it for 90% of the games. I do agree that some HUGE games might need to delete and load things though.

baxslash, I find that odd, that the speed increase when you don't have the menu loaded. Do you have an easy example project so I can test that on my devices? If it's so, then yes, DO delete!

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 10:12
I don't have time right now but I will do some tests soon. I know I was getting 50fps before and 60 afterward...


this.mess = abs(sin(times#))
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Aug 2012 10:40
Quote: "Hodgey, when you eat, do you try to use as little of the area of the plate as possible"

I do however I don't have to worry about other people putting their food on my plate. While you can easily get away with loading all of your media at once in a small game I just don't like the fact that it's taking up more memory than it needs to at that point in time. It's more of an ethical/practice thing than a technical thing.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 11:08
It may or may not make a huge difference but as Hodgey suggests it is surely worth trying to make the leanest code possible and use the least amount of resources as a matter of course rather than whether it's necessary in this one particular instance. Sure, if you're only making small simple games then it doesn't matter at all but if you move up to larger games and using bigger resources (particularly when 3D comes into play) you will soon find every Mb counts.


this.mess = abs(sin(times#))
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 13:47
Yes, but to me it seems like people create and delete sprites and images all the time, like every time a menu pops up or some info box. I would think creating and deleting the items on the fly would eat processor time, and the constant memory re-allocations would be prone to memory leaks, and fragmentation. I'm not sure what garbage collection routines AppGameKit has, but I hope they are handling the memory well.

As I said, 90% of games will not be a problem on 90% of devices, but large games, should load data between main segments. Just like in the old days when you got the message "Insert disk 2"

If you have 10MB of images loaded, and 10000 sprites created, then there should be only around 11MB load on the memory, since every sprite is only referring to those media, not duplicating them in memory, and the data for a sprite mostly is a few bytes, maybe 100 bytes? with all the values a sprite contains.

Having allot of sprites on screen at the same time only affects speed, if they are overlapping. If you have a background, and then fills the screen with transparent sprites, then the renderer has to render 2 screens worth of pixels at every sync(). So adding a HUD with a small frame and a big transparent window in it, sucks up 1 whole screen worth of pixels to process. If you have your help screens in the background, then they get rendered, and speed is affected. If you move them off screen, or make them inactive, the render pipeline should skip them. Maybe this is your problem baxslash?

If the fact that a sprite is defined, is enough to slow things down, then TGC need to look at it's code, it makes little sense.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Aug 2012 13:55
You have a fair point Impetus. To each his own.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 13:55
It's possible something else was the cause and I don't pretend to know all there is to know about memory usage.

If you're right then that's great, but I'd still prefer to only keep things in memory when necessary. As 7RS said:
Quote: "Anyway you can write your own code, i'm not in the position to teach anyone how to program. You have your idea and knowledge and i have mine. I prefer to remove unused objects and to recreate them when is necessary."



this.mess = abs(sin(times#))
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 14:04 Edited at: 3rd Aug 2012 14:07
Yes, we agree to do things differently. My original question was to understand why you did as you did, and I have my answers. I will only change my ways when and if needed, I only seek to know why people do things differently than me. At the same time I wonder if there is a bug in AppGameKit we need to hammer out. I'll do some tests and see what affects memory and speed.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Aug 2012 14:10
@ baxslash: ninja'd

Quote: "At the same time I wonder if there is a bug in AppGameKit we need to hammer out. I'll do some tests and see what affects memory and speed."

That's going to bounce back to me eventually but I'm curious to see the results of those tests so keep us updated.

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Aug 2012 14:22
AGK Pascal is good on this one. When you load a new scene all the objects in the previous scene are automatically freed (unless you ask it not to!)

For example - if your main menu is a "scene" you can choose whether to keep it in memory or simply recreate it next time it's visited.

-- Jim
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 20:51
ok, I did some extensive testing.

I used a 442Kb 800x600 PNG image, and a Samsung Galaxy S II

The player on it own used from 7.08MB to 7.16MB between runs, probably because of memory fragmentation issues.

The test program with no media loaded, and no sprites used 8.75MB, and had 65FPS.
1 image loaded, 0 sprites = 10.28MB / 65FPS
2 images loaded, 0 sprites = 10.26MB / 60FPS (Less memory. Go figure)
4 images loaded, 0 sprites = 10.87MB / 60FPS
4 images loaded, 1 sprite = 11.90MB / 60FPS
4 images loaded, 10 sprites = 11.91MB / 60FPS
4 images loaded, 100 sprites = 11.98MB / 17FPS
4 images loaded, 500 sprites = 12.32MB / 5FPS

4 images loaded, 500 sprites (visible off) = 12.20MB / 60FPS

100 images loaded, 100 sprites (visible off) = 91.28MB / 60FPS

That's 100 unique images 800x600 pixels, and 100 sprites at 800x600 pixels, and i'm still having 450MB free on my phone.

If I started to delete all 500 sprites, and recreate them in every sync() I found that NO memory were freed, in fact it just started to eat away, and the memory usage went from 12.17MB to 12.42MB in just 10min. This is an extreme example, but it proves that every time you delete a sprite, you WASTE memory

I then set up to delete and load the first image, in every sync(), and that too ate memory, but only at a rate of around 0.03MB / minute, meaning the image data memory is recycled, but the image metadata is not garbage collected, and you waste memory.

So my conclusion. Delete and reload images when needed, that saves ram, but makes a very small memory leak too. Delete sprites if you have to, but that ALWAYS create a memory leak, and the amount of ram your app uses increase for each sprite you delete. It's better to just make them inactive. The amount of inactive sprites had no practical effect on the FPS.

The number of images loaded, and sprites created, has no effect on FPS, as long as they are visible off! If you hide them in the background, it has horrible effect on the FPS.

Now, I need to test if variables and arrays leak memory also...

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 21:40
Well it seems I must be wrong. Perhaps I changed something else that was causing the slowdown!

There are some worrying results there Impetus73, I think this warrants further testing. Are you able to test iOS?

I don't mind not deleting images if there's hardly any difference but I NEED to delete sprites regularly. Most / all of my games rely on it...

My particle systems it seems could bring any device to a halt eventually!


this.mess = abs(sin(times#))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Aug 2012 21:56
Quote: "My particle systems it seems could bring any device to a halt eventually!"


I'm going to go out on a limb and suggest that it is the memory leak from deleting sprites and the number of sprites you are probably using in your particle systems that is causing this.

You might be better off just making the sprites invisible and inactive when not needed. When it comes time to create a sprite, check to see if it already exists first. This would require keeping an array or using a predefined set if ids. But should be totally doable.

It's what I do with the few sprites that I know are reused.

Cheers,
Ancient Lady
AGK Community Tester
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 22:23
I'll test on iOS, when I get my iPad back this weekend, it's on loan to a friend

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Aug 2012 22:27
I also found that if you recreate an existing sprite, you also leak memory. that is, if you use the createsprite() command on an existing sprite handle.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 3rd Aug 2012 23:37
Memory leaks when releasing resources(images/sprites/etc) via the appropriate API function/command(i.e. deletesprite/deleteimage) is almost inexcusable. I'd much rather take a CPU hit and no memory leak over the opposite.

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Aug 2012 23:45
Lazarus running in debug mode is scarily aggressive about memory leaks. It's good, but frightening, if you see what I mean: exit app - get a whole stream of error reports. Concentrates the mind somewhat!

-- Jim
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 4th Aug 2012 12:15
The image data is released, when deleting images, but the metadata following the images and sprites (Who has no image data), is just ignored, and the memory is not reclaimed. But you must remember that you would have to delete around 60 images/sec for 33min to loose 1MB memory. So the effect is neglect able for most apps, and I guess that's why the space is not reclaimed. To improve speed.

I would like to see a command like " defragmemory() " that one could call and force a defragmentation/garbage collection of wasted memory. This would take a few processor cycles, but would prevent memory leaks to grow out of hands.

If you delete 10 sprites, then make 1 new, it just start using memory at the imagined "memory segment 11", not reusing the 10 deleted ones.

I just tested dim / undim, and that reclaims the memory, but at some intervals. I ran the following code on my Galaxy:



This loop has no sync() so it runs wild, and the memory the app used, jumped around from 50 to 20MB up and down. and then using the sync() the frame rate dropped to 14FPS, meaning all this allocation and reclaiming of arrays takes time. Again this is an extreme example test, So deleting arrays is safe

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.

Login to post a reply

Server time is: 2024-04-27 18:48:35
Your offset time is: 2024-04-27 18:48:35