Clearing memory


(Bimbamm) #1

I have a certain Entity which is created on click and recycled seconds later. On every click the memory goes up by 0.005 - 0.02. My game depends heavily on mouse clicks. so after thousands of clicks my memory is multiplied by several times of its original memory usage. I fear this will cause lag or even crashes. Is there a way to clear clear unused memory periodically?


(Zachary Lewis) #2

The garbage collector will automatically get rid of unneeded references when it has a chance.

Have you clicked many times then waited for a minute without doing anything? What happens?


(Jonathan Stoler) #3

Also make sure that no other object(s) are using up the memory. Try creating a completely clean project with a really empty world except for this one object, and see if it still has problems.

I spent a whole day debugging a supposed memory leak in one of my objects; turns out it was just the game loading up huge mp3 files, totally unrelated to the object in the first place. :confused:


(Bimbamm) #4

Thanks for the answers. The reason my memory increases is a mistake in the the create() function. public class CriticalFadingNumber extends Entity

	public function CriticalFadingNumber() 
	{
		text = new Text("text");
		graphic = text;
		x = Input.mouseX;
		y = Input.mouseY;
	}
	override public function update():void		
	{
                text.alpha -= 0.02;
		y -= speed;
		speed -= 0.05;
		if (text.alpha <= 0)
			{		
				world.recycle(this);					
			}
		super.update();
	}
	public function init():void
	{
                   text.alpha = 2;
                   speed = 2;
		text = new Text("text");  // unnecessary 
		graphic = text;              // unnecessary
		x = Input.mouseX;       
		y = Input.mouseY;       
	}		
}

In the main code I use the following code

CriticalFadingNumber(create(CriticalFadingNumber)).init();

The reason for the memory leak is that the graphic is recreated every time, which is not necessary when recycling.