Skip to content

Fix Box2D memory leak#203

Merged
andyleejordan merged 2 commits intomasterfrom
fix-leak
Apr 15, 2015
Merged

Fix Box2D memory leak#203
andyleejordan merged 2 commits intomasterfrom
fix-leak

Conversation

@andyleejordan
Copy link
Copy Markdown
Collaborator

A cursory review of Box2D and our use of it lead me to figure out that the leaked bodies contain references to our objects (creatures, projectiles, food, etc.) that are set using setUserData(this).

Rather than hack the library to fix its leaks, instead, in each class's respective killBody() function, I now setUserData(null) to remove the reference to our objects.

Since the bodies are still leaked and sometimes even come into "contact," I had to check if either body is null in our endContact() function and simply return if that is the case.

This has dropped our memory usage significantly: less than 50 MB after 16 generations.

See the bottom of my presentation for screenshots of this fix in action.

This finishes #200 and #186.

Any additional body types added to the game must follow the same pattern detailed here.

A cursory review of Box2D and our use of it lead me to figure out that
the leaked bodies contain references to our objects (creatures,
projectiles, food, etc.) that are set using `setUserData(this)`.

Rather than hack the library to fix its leaks, instead, in each class's
respective `killBody()` function, I now `setUserData(null)` to remove
the reference to our objects.

Since the bodies are still leaked and sometimes even come into
"contact," I had to check if either body is null in our `endContact()`
function and simply return if that is the case.

This has dropped our memory usage significantly: less than 50 MB after
16 generations.
@andyleejordan
Copy link
Copy Markdown
Collaborator Author

@BIOEvolveTD I believe this demonstrates that my hypothesis was correct, and the presentation is a pretty detailed write-up. I can link to it in our wiki if you'd like. Where would it go?

@tsoule88 This ended up being a very elegant solution that does not, as far as I can tell, introduce any new issues, nor did it require anything very "hacky." We simply remove the references to our objects when we're done with them.

@higles Please test this for me on your machine, as it's a different platform than mine.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

Oh and @dstreett, like you surmised, it was simple once I figured it out. :neckbeard:

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

Awesome work! Not sure where it would go on the wiki, but I feel strongly that it should be on there somewhere. Terry?

Is this merged with master now?


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 7:20 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: Re: [evolvedTD] Fix Box2D memory leak (#203)

@BIOEvolveTDhttps://github.com/BIOEvolveTD I believe this demonstrates that my hypothesis was correct, and the presentation is a pretty detailed write-up. I can link to it in our wiki if you'd like. Where would it go?

@tsoule88https://github.com/tsoule88 This ended up being a very elegant solution that does not, as far as I can tell, introduce any new issues, nor did it require anything very "hacky." We simply remove the references to our objects when we're done with them.

@higleshttps://github.com/higles Please test this for me on your machine, as it's a different platform than mine.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93155247.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

Ha! I thought that usage looked funky. I knew I needed to test more, but I'm excited! The memory leak is still fixed, but the usage is actually ~500 MB on average.

I would like some more testing to be absolutely certain this fixes it.

Granted, the bodies still leak, so there will always be a slight memory leak, but without objects being cleaned up, I think we can leave it.

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

I'm running it now, and it looks like 1.8Gb of usage at Gen 12 for me, but not crashing yet.


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 7:52 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: [BULK] Re: [evolvedTD] Fix Box2D memory leak (#203)

Ha! I thought that usage looked funky. I knew I needed to test more, but I'm excited! The memory leak is still fixed, but the usage is actually ~500 MB on average.

I would like some more testing to be absolutely certain this fixes it.

Granted, the bodies still leak, so there will always be a slight memory leak, but with out objects being cleaned up, I think we can leave it.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93164113.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

Generation 28:
fixed-usage

Sadly this does not fix it 100% because the library itself still leaks body objects. But I believe it takes care of, say, 90% of the leak.

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

Generation 29, 2.16 Gb, still running.


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 8:00 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: Re: [evolvedTD] Fix Box2D memory leak (#203)

Generation 28:
[fixed-usage]https://cloud.githubusercontent.com/assets/2226434/7151557/c75b3b84-e2e0-11e4-869c-51e391625d01.png

Sadly this does not fix it 100% because the library itself still leaks body objects. But I believe it takes care of, say, 90% of the leak.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93167537.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

@BIOEvolveTD My guess is that you're seeing the allocated heap size (orange in my graph) and not the used size (blue), which would line up with what I'm seeing.

We don't really want to have to fix the library. And I think replacing the world each wave just to clear the leaked bodies may be a bit drastic; but if it comes to it, we know what to do.

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

I'm positively giddy that I can run this thing to generation 30!


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 8:02 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: Re: [evolvedTD] Fix Box2D memory leak (#203)

@BIOEvolveTDhttps://github.com/BIOEvolveTD My guess is that you're seeing the allocated heap size (orange in my graph) and not the used size (blue), which would line up with what I'm seeing.

We don't really want to have to fix the library. And I think replacing the world each wave just to clear the leaked bodies may be a bit drastic; but if it comes to it, we know what to do.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93168646.

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

finally crashed at generation 37. A record run for me.


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 8:02 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: Re: [evolvedTD] Fix Box2D memory leak (#203)

@BIOEvolveTDhttps://github.com/BIOEvolveTD My guess is that you're seeing the allocated heap size (orange in my graph) and not the used size (blue), which would line up with what I'm seeing.

We don't really want to have to fix the library. And I think replacing the world each wave just to clear the leaked bodies may be a bit drastic; but if it comes to it, we know what to do.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93168646.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

Ah! Damn it Box2D, why-you-leak-so-much.

If you only got to 37, although a massive improvement, we may still need to take measures to eliminate the leaked bodies. I am looking into it now.

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

I'm at 30+ minutes and 50 generations, which is long enough to clearly see the memory leak of the Box2D bodies themselves.
mostly-fixed

I think this fix should definitely be merged now so people can get on with Tuesday's assignment without as many issues, with a better fix forthcoming.

@BIOEvolveTD
Copy link
Copy Markdown
Collaborator

agreed.

The good news? In one run I clearly see evidence of stabilizing selection on armor, with an optimum armor value of 11.


From: Andrew Schwartzmeyer notifications@github.com
Sent: Tuesday, April 14, 2015 8:15 PM
To: tsoule88/evolvedTD
Cc: Robison, Barrie (brobison@uidaho.edu)
Subject: Re: [evolvedTD] Fix Box2D memory leak (#203)

I'm at 30+ minutes and 50 generations, which is long enough to clearly see the memory leak of the Box2D bodies themselves.
[mostly-fixed]https://cloud.githubusercontent.com/assets/2226434/7151666/f76a32b0-e2e2-11e4-80ba-9e0381e233c7.png

I think this fix should definitely be merged now so people can get on with Tuesday's assignment without as many issues, with a better fix forthcoming.

Reply to this email directly or view it on GitHubhttps://github.com//pull/203#issuecomment-93172084.

andyleejordan added a commit that referenced this pull request Apr 15, 2015
Fix Box2D memory leak (mostly)
@andyleejordan andyleejordan merged commit 2c7bfd9 into master Apr 15, 2015
@andyleejordan andyleejordan deleted the fix-leak branch April 15, 2015 03:20
This was referenced Apr 15, 2015
@Programming-Chaos
Copy link
Copy Markdown
Owner

Fantastic. There’s probably a note (or several) on-line somewhere reminding users to set userdata to null that I missed. A link from the wiki is a good idea. What about a new page, from the main page, on the coding process in general (I have some comments that could go there including a brief discussion of our choices to use Processing, github, etc.) and a link from that page to this particular issue?

@andyleejordan
Copy link
Copy Markdown
Collaborator Author

@tsoule88 Ha, I don't think they intend for this to have to be done. From the library for destroyBody()

Destroy a rigid body given a definition. No reference to the definition is retained.

It is clear that there shouldn't be a leak of these bodies (further research showed that Box2D waits until the next step to actually destroy them), but there is. So no worries, this was a workaround we had to find ourselves.

I am all for a coding process page! Great idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants