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

How to clear the cloned frame correctly? #1574

Closed
4714407 opened this issue Jan 24, 2021 · 11 comments
Closed

How to clear the cloned frame correctly? #1574

4714407 opened this issue Jan 24, 2021 · 11 comments

Comments

@4714407
Copy link

4714407 commented Jan 24, 2021

When I use the clone method on frame, I find that the memory has been increasing and has not been released. I see that the comment of the method says:"Expect a heap overflow exception when using this method without cleaning up."
Can you tell me how to clear the frame object correctly?
I would like to ask, in addition to passive garbage collection, is there any way to manually free memory?

@saudet
Copy link
Member

saudet commented Jan 25, 2021

The underlying methods use Pointer to allocate memory, so we can use PointerScope in the fashion introduced here:
http://bytedeco.org/news/2018/07/17/bytedeco-as-distribution/

@saudet
Copy link
Member

saudet commented Jan 25, 2021

Ideally though it should probably implement AutoCloseable to let us deallocate manually in such instances.

@saudet
Copy link
Member

saudet commented Jan 25, 2021

However, that's going to require adding reference counting for pointers in other areas of FrameConverter, FrameGrabber, FrameFilter, and FrameRecording, so let's add that to the "to-do list".

@4714407
Copy link
Author

4714407 commented Jan 25, 2021

Thank you for your reply.

The frame I cloned is passed to the child thread for use, so what if I use this PointerScope

@saudet
Copy link
Member

saudet commented Jan 25, 2021

That's fine, you can pass the PointerScope to the thread as well, possibly after calling PointerScope.extend().

@4714407
Copy link
Author

4714407 commented Jan 25, 2021

Can you give me a sample code? I'm sorry, I still don't know how to use it

@saudet
Copy link
Member

saudet commented Jan 25, 2021

@4714407
Copy link
Author

4714407 commented Jan 25, 2021

Please help to see if the following code is correct?

while ((frame = grabber.grabImage()) != null) {
   if (frame.image != null) {
        try (PointerScope scope = new PointerScope()) {
                            final Frame finalFrame = frame.clone();
                            final PointerScope extendedLocalScope = scope.extend();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter();
                                    BufferedImage bufferedImage = java2dFrameConverter.convert(finalFrame);
                                    //do something
                                    extendedLocalScope.close();
                                }
                            }).start();
                        }
   }
}

@saudet
Copy link
Member

saudet commented Jan 25, 2021

Yes, that should work... But if you have a lot of those frames, you should create single instances of everything and reuse them.

@4714407
Copy link
Author

4714407 commented Jan 25, 2021

It works. Thank you very much!

@4714407 4714407 closed this as completed Jan 25, 2021
@saudet saudet reopened this Jan 25, 2021
saudet added a commit that referenced this issue Mar 3, 2021
@saudet saudet removed the help wanted label Mar 3, 2021
@saudet
Copy link
Member

saudet commented Mar 10, 2021

The Frame class in JavaCV 1.5.5 now comes with a close() method for that purpose. Enjoy and thanks for reporting!

@saudet saudet closed this as completed Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants