Delete a instance of class

Monkey Forums/Monkey Programming/Delete a instance of class

xzess(Posted 2011) [#1]
Hi there,

is there a possibility to remove an instance of a class i created with "new" ?


ziggy(Posted 2011) [#2]
Dereference it. Monkey us Garbage collected, no need to manually delete instances fo classes.

Class MyClass
End Class

Local var1:= New MyClass 'We create one instance here, pointed by 1 variable
Local var2:=var1 'The same instance is now pointed by 2 variables
var1 = Null 'The same instance is now pointed just by one variable (var2)
var2 = Null 'The instance is not pointed by any variable, so Monkey will free it for you in the background.


When a variable runs out of scope it is nullified. All in all, this design makes it very difficult (impossible?) to have memory leaks in code.


xzess(Posted 2011) [#3]
Thanks ziggy, Well plz check it out:
Http://www.xzess.org/Webgames/Exhale/

Obviously i have a memory leak :/
Every New map everything Gets reset and nulled.


marksibly(Posted 2011) [#4]
Hi,

Why 'obviously'?

It seems to work fine here, although I'm not sure what I'm supposed to do.

[edit]
It does 'stutter' a bit when a new fleet of rockets is launched, but catches up and smooths out soon after that.
[/edit]


xzess(Posted 2011) [#5]
Don't understand me wrong, the problem could lay on my side. I'm just seeking a solution and im trying to find out where the problem is. You are doing a great job Mark!
I will take a closer look this evening


xzess(Posted 2011) [#6]
You have to let it run. After Some minutes memory is about 4gb. On iOS it crashes before the map is over. On android and glftw it crashes immediately on app launch


Hima(Posted 2011) [#7]
Instead of creating a new instance of the rocket everytime, I think you should recycle it. The game crash on me when the red planet launch HUGE number of rockets.

One thing about garbage collect is that most of the time it doesn't delete them right away but sometimes later. So in the long run you don't have to worry about memory but looking at your game it seems like you want the memory as soon as possible for those rockets. In that case, I think you should go with Object Pool design pattern where you recycle an unused object instead of creating a new one everytime.


marksibly(Posted 2011) [#8]
Hi,

JavaScript is 100% responsible for memory management so there *shouldn't* be any memory leaks!

Are you definitely 'releasing' all objects?

The only thing I can think of is that you've got a bunch of objects sitting in a list/map somewhere you've forgotten about...


xzess(Posted 2011) [#9]
I tried this before v40 and it crashed it much sooner.
I will give recycling a chance again. Hopefully that's it. Thanks for pointing out when it crashes


xzess(Posted 2011) [#10]
In the code of the version I posted in URL it does the first release when a ship arrives it's destination. The ship instance gets nulled, img discard, and removed from the list.
On newGame (won/lost/button clicked) all vars get reset and all lists cleared.
I will add a check if there are no ships travelling, the list get cleared also


marksibly(Posted 2011) [#11]
Hi,

Why are you discarding the image?

All images should preferably be loaded just once in OnCreate.


xzess(Posted 2011) [#12]
I have 2 versions, 1st compiled before release of v40 with object recycling and 4 lists (allships,allplanets,activeships player and same for CPU)
So oncreate it preloads 1000 ships + planets + bg, music etc
This crashes much sooner as if I do it like in v2 where I dynamically add and release the ships. I have to say that I know that this isn't sweet but it was a try because preload and recycle didn't make it better

Edit: I will preload those 2 fighter images as i did before so there won't be a need of discarding the image. the memory leak should be then caused by the lists. I will check that


marksibly(Posted 2011) [#13]
Hi,

> So oncreate it preloads 1000 ships + planets + bg, music etc

Why 1000 ships?

You should be sharing a single global ship image with 1000 ship objects.


xzess(Posted 2011) [#14]
Yes it's one image but 1000 vars. Just typed wrong sry


Dima(Posted 2011) [#15]
It's really difficult to tell what's happening without looking at the code. A few things I noticed within the very brief time i spent with it;

in task manager the instance of chrome browser that uses the game takes between 30-50MB and this number fluctuates meaning SOME memory does get released at some point.

I was able to get mem usage up to 300MB by spamming the New Map button while many ships are flying about, but then it reset back to 40MB. BTW, clicking the New Map DOES NOT CLEAR THE SHIPS - they keep flying to empty space where planet used to be in previous map, so if that's not intended maybe you could check that out further.

After a few New Map clicks i notice that ships appear to come from random places and sometimes in huge numbers, not sure where they are coming from, but my guess is where a planet used to be in prev map. Are the ships supposed to keep appearing without any interaction? even after pressing New Map?

Now I got the game to 600MB and it froze, that's after I was typing this and the game ran by itself with ships flying about by themselves. It seems that not everything is cleared properly at this time.

i also noticed the debug 'error: delta < 0' im not sure if thats delta time or another delta, but delta time should NEVER be 0 or less. Also, make sure you are not dividing by 0 ANYWHERE (specially if you're using delta time), that would cause crashes.

These are just observations, but without source it's very very hard to know. maybe you could post just a small region of your code where you add objects to lists and where you remove them?

Cheers,
Dima


Virtech(Posted 2011) [#16]

When a variable runs out of scope it is nullified. All in all, this design makes it very difficult (impossible?) to have memory leaks in code.



Does this also apply to stack items that hold pointer to a particular object?

Usually If I want to remove an object referenced in a stack I do something like this:
' Remove when health=0
For index=0 to asteroids.Length()-1
	Local asteroid:=asteroids.Get(index)
	If asteroid.health<=0
		xplosions.Push New Xplosion( asteroid.x, asteroid.y, (asteroid.s*4) )
		asteroids.Remove(index)
	EndIf
Next


As shown above I just use Remove(index) to remove the object reference. I don't set anything to Null.

Is this method correct, or should I also set the local index variable "asteroid" = Null?


ziggy(Posted 2011) [#17]
Removing the Index, removes the contents of the Stack item, that is, decreases by one the number of references to the given object. If the object is "only" stored in the stack, it'll be free.


Jesse(Posted 2011) [#18]
accord to this:

	Method Remove( index )
		For Local i=index Until length-1
			data[i]=data[i+1]
		Next
		length-=1
	End

if index is the last item in the stack the item is never removed only the stack length variable is decreased.
it's the same way "pop" works, only the length variable is decreased but it is over written when a new object is added to the stack again. so, if your stack gets to be like 1000 objects and the stack gets decreased one at a time, always the last one, until the index is 0 you will have 1000 objects floating around wasting memory until each is over written by inserting or "push"ing 1000 items to the stack again.
I think it needs to be something like this:
	Method Remove( index )
		If index = length-1
			data[index] = Null
		Else
			Local i
			For i=index Until length-1
				data[i]=data[i+1]
			Next
			data[i] = Null
		Endif
		length-=1

	End