Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of memory error with simple fix #147

Open
rakar opened this issue Jan 10, 2020 · 3 comments
Open

Out of memory error with simple fix #147

rakar opened this issue Jan 10, 2020 · 3 comments

Comments

@rakar
Copy link

rakar commented Jan 10, 2020

I noticed that after running for a while (much more than an FRC match) my pipeline code would eventually crash due to an Out of Memory error. I corrected this by adding a System.gc() line to the endless loop at the bottom of Main. As expected the free memory on the webpage drops every second for 10 seconds and then cycles back to the original number and starts dropping again. All good after an overnight run.

// loop forever
    for (;;) {
      try {
        Thread.sleep(10000);
        System.gc();
      } catch (InterruptedException ex) {
        return;
      }
    }
@ThadHouse
Copy link
Member

What is likely happening is you are allocating either Mats or other native data structures during pipeline execution, rather then only before loop execution. The GC doesn't know how to properly handle these native resources, and gets confused. Anything allocated during the loop, if it has a close method should have closed called at the end of the loop, or placed in a try with resources. that is a much more reliable and better option then manually forcing the GC to run.

@rakar
Copy link
Author

rakar commented Jan 10, 2020

I do clone a mat which I was deliberately not releasing. My concern was that I didn't know if the mat I was creating would still be in use when I released it (due to an imageSource.putFrame(output) call). I have tried releasing it and it seems to work, but my, limited, understanding is that in general one can't be sure that a mat is no longer in use when passed off to some black box routine in the opencv world.
The memory usage with the release in place and the gc() gone is wonderfully flat-line. I just want to be sure that this is generally considered safe.

@ThadHouse
Copy link
Member

putFrame does explicitly make an internal copy so it is safe to free the mat after that. Same with something like imshow. The functions that display usually make a copy internally specifically for this reason.

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

No branches or pull requests

2 participants