-
Notifications
You must be signed in to change notification settings - Fork 869
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
gocv code memory leak #1190
Comments
my gocv version is 0.36 and opencv version is 4.10.0 |
There isn't enough information provided to be able to help so if you can't show us code then would you create a goroutine which dumps the MatProfile every 5 minutes for at least a 15 minute period and post the output here. This is untested code, but something like.
|
hello swdee Do I need to run this asynchronous code only while the gocv program is running? If so, since gocv is constantly allocating and releasing Mat memory, will the number of unreleased Mat objects always be greater than 0 during the execution of the gocv program? |
Yes just run it temporarily to capture the output on your gocv program. For clarity I should note the dumpMat code I wrote above needs to be added to your gocv program. As for your second question, we need to see the output to see whats happening. However for efficiency you should not be opening and closing Mats all the time, instead they should be considered preallocated memory spaces that OpenCV manages in the background. For a simple example you should be doing.
When you run the MatProfile against the above code it will only show a single Mat being open. Whilst the code still works, its inefficient to do the following as it is allocating and deallocating memory all the time;
The above MatProfile will probably show zero Mat's and if you forget the |
Yes, when my service had no requests, I ran the following code every half hour, making a total of four runs.
But each time the result output is:
But the memory still hasn't been released. |
If your showing 0 Mats then there is no memory leak from GoCV. The memory usage graph you posted is probably Go's ordinary memory usage (unrelated to GoCV) which does not get released immediately. Go's pprof will help you confirm that. |
The addresses changing is ok as your program could open a Mat, close it, and when the next time that code function is called it would then open a new Mat and close that. Each Mat would have a different address. Its a bit hard to tell from your screenshot, so can you copy/paste text, this would be more useful. Also can you show the output captured every 10 minutes for multiple entries to see how the Mat profile total count changes over time. There being 53 Mat's open does not necessarily mean those are leaked Mats, rather those are just ones open when the profile was taken. If that number increases over time and you have 1000's of Mats open then you would expect some part of your code to be leaking memory as it has not closed a Mat. |
I noticed that the memory usage is still slowly increasing. |
Ok, can you show us the code from file /go/src/linuxapp/logic/denoiseHandel.go particularly the function CallModelWithCrop(). Would need to see the code where it creates the Mat, how it uses it, and where its closed. |
I ran the memory leak detection code and found that the line subImg = gocv.NewMatWithSize(InputSize, InputSize, inputMat.Type()) has a memory leak. where in use ·· func (d *DenoiseHandelImpl) ReleaseSubResource(ctx context.Context, subPicInfos []*SubImgInfo) {
} |
@swdee thank you very much |
On this code
It looks to me that your copying inputMat to subImg, then later creating a NewMatWithSize to subImg. However inputMat is being closed whilst subImg is left open. Hence the memory leak. So I gather you have resolved this issue now? |
subImg is subsequently assigned to subImgInfoList and will be closed collectively later. |
the gocv IMDecode causes memory leak, I face the same problem |
about gocv code memory leak
Description
Due to business scenarios and company regulations, I cannot make the code public.
Our project's main work is to split an image into multiple 512*512 images, send them to an AI model, and then paste the images returned by the AI model back into the original image. The gocv functions used are listed in the table below.
Below is the memory usage graph of a single request test environment. It shows that the memory does not completely return to its initial state, indicating a memory leak.
In the production environment, user requests also exhibit noticeable transition phenomena.
I have tried running the following code to detect memory leak issues, and found that the number of leaked mats is 0.
How should I continue investigating the subsequent issues?
The text was updated successfully, but these errors were encountered: