Jump to content

Possible Memory Leak? Hash property keeps growing


Recommended Posts

In my game, I have some parent groups where I store other groups and/or sprites. When I destroy the children using .destroy, they are removed from the property children, but they stay in the property hash of the parent group, forever. Therefore the game's memory usage grows over time until crashing the browser.

Is it normal for children not being removed from hash property? What is that property for?

Thanks in advance


EDIT: Found the cause. See last post.

Link to comment
Share on other sites

5 minutes ago, samme said:

They should be removed from the hash when destroyed. That's weird.

All the same, an ever-growing hash would have to be enormous to cause memory problems. If you're creating/destroying sprites very frequently, that might be the true cause.

I couldn't reproduce this with a simple group of sprites, the hash length is always accurate:


The game is this one

You can check the hash of the food group using chrome console: 


And compare with the children property: window.game.state.states[this.game.state.current].foodGroup.children.length

Yes, I'm creating a new food for every food that is spawned nearby, and destroying them after eaten or out of the fov. Shouldn't I do this?

Link to comment
Share on other sites

1 minute ago, samme said:

Oh, I see, it's a long-running game, that makes more sense.

Have you tried using Group instead of SpriteBatch?

Does foodGroup use physics?

Yes, I've tried.

The physics are decoupled from the graphics. Every object has a body property where I store a p2 body, and destroy it when I destroy the parent.

Link to comment
Share on other sites

1 hour ago, samme said:

Try setting a breakpoint on addToHash to see what is actually adding to it.

Step into your destroy call and see if it reaches removeFromHash.

You can of course try

foodGroup.hash.length = 0


I'll try that and report.

Whats the use of hash by the way?  Emptying it manually doesn't affect existing entities?

Link to comment
Share on other sites

Oops, I found the cause.

Each time I created an sprite/group, I first created it and then added it to a parent group with group.add, but the child "parent" property always remained as world.

I solved it by setting the parent parameter in the constructor method.

Link to comment
Share on other sites

Another note, if I move a group from one group to another one, with group1.remove and group2.add, when I destroy the child group at the end, it won't be removed from it's last parent's hash property (group2), so I had to set group2.hash.length = 0 after moving a child.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...