Memory Increasing Slowly


(Zouhair Elamrani Abou Elassad) #1

Hi again, i have a memory problem and i can’t see where it’s coming from, i add entities to my world (like 550 entity), but that’s not the problem, once i add them all bu reading coordinates from a script the memory is almost 300M, when i play the game i notice that the memory is increasing bu 0.01M almost every 30s, i removed all entities and kept just the player, the memory was stable and not increasing, i added one simple type of entities then the memory got back to increase again :

public class Level extends Entity 
{
    [Embed(source = "../../assets/images/tile.png")]
	protected static const TILE_ART:Class;
	
	protected var _levelImage:Image;
	
	public function Level(xPos:int) 
	{
		
		this.x = xPos;
		this.y = 0;
		
		initLevelImage();
		
		layer = 3;
		type = "level";
	}
	
	
	override public function update():void 
	{
		if (x < FP.camera.x - 1000) {
				world.remove(this);
		}
		
		if (Globals.playerAlive == false) {
			active = false;
		}
		
	}
	
	private function initLevelImage():void {

		_levelImage = new Image(TILE_ART);
		mask = new Pixelmask(TILE_ART, 0, 0);

	}

I even removed the update method and the mask but nothing changed !


(Zouhair Elamrani Abou Elassad) #2

I was wrong, even if i removed most of the entities memory keeps increasing :s.


(rostok) #3

Every time you create an Image from a class two objects that take memory are created. First one is Bitmap by FP.getBitmap() this one is cached by Flashpunk so there’s only one such Bitmap per class. That’s not that bad because it needs to be created to use it. However the Image itself creates a buffer that stores how it looks like after basic transformations (color, flipping). Image’s buffer is created with every new image object. So basically if you are not making any transformations on entity’s image good optimisation would be to use exactly one same, single image for every such entity (you can pass it as reference to constructor). That would keep your memory usage at constant level.

If that’s not the case: divide and conquer. Comment as much as you can and try to find code responsible for memory being allocated. Then drill down…


(Jacob Albano) #4

None of your memory problems are strictly related to Flashpunk – meaning that the leaks you’re dealing with are coming from your code, not from the engine. Short of spotting any glaring issues, there’s a limit to how much help we can be without reviewing your entire codebase. I recommend you invest some time working with a profiler and see what objects are being allocated the most. Flashdevelop has one built in, and you can also look into Adobe Scout and TheMiner.


(Zouhair Elamrani Abou Elassad) #5

Thank You, i’ve checked The Miner, it’s not working great, i’ll check Adobe Scout.