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

Memory leak in PyTurboJPEG? #12

Closed
trdastidar opened this issue Sep 17, 2019 · 6 comments
Closed

Memory leak in PyTurboJPEG? #12

trdastidar opened this issue Sep 17, 2019 · 6 comments
Assignees

Comments

@trdastidar
Copy link

Hi,

There seems to be a case of unmanaged memory in PyTurboJPEG. Memory is allocated during the image decode process, but not deallocated later (at least not immediately), and the memory usage of the process keeps building up.

As an example, please consider the following code:

from turbojpeg import TurboJPEG, TJPF_GRAY, TJSAMP_GRAY
import psutil
import os
import time
jpeg = TurboJPEG("/usr/lib/x86_64-linux-gnu/libturbojpeg.so.0")
import numpy

i = 0
PROCESS = psutil.Process(os.getpid())

buf = None
with open('some_image.jpg', 'rb') as fin:
    buf = fin.read()
for i in range(100):
    print 'iteration', i
    img_array = jpeg.decode(buf)
    print img_array.shape
    print 'memory = ', PROCESS.memory_info().rss // 1024

The output of this program looks like this:

...
iteration 43
(2160, 4096, 3)
memory =  806464
iteration 44
(2160, 4096, 3)
memory =  832576
iteration 45
(2160, 4096, 3)
memory =  858472
iteration 46
(2160, 4096, 3)
memory =  884340
iteration 47
(2160, 4096, 3)
memory =  910188
iteration 48
(2160, 4096, 3)
memory =  936316
iteration 49
(2160, 4096, 3)
memory =  962216
iteration 50
...

The memory usage keeps building up till more than a GB. It sometimes goes back to a low value, but again the buildup happens.

The other popular turbojpeg python wrapper -- jpeg4py -- doesn't have this problem. However, I prefer using PyTurboJPEG because it offers a scaling factor in the decode function which speeds up things considerably for my use case. But the memory build up is causing problems as I have a tight memory budget.

What could be the reason and are there any known fixes/workarounds?

@lilohuang
Copy link
Owner

lilohuang commented Sep 17, 2019 via email

@trdastidar
Copy link
Author

Thanks. I will await your reply.
Both PyTurboJPEG and TurboJPEG are at the latest versions (1.4.2-0ubuntu3.1).

@lilohuang
Copy link
Owner

lilohuang commented Sep 17, 2019 via email

@trdastidar
Copy link
Author

I don't think the memory leak is at the TurboJPEG level, as the jpeg4py python wrapper works fine without any memory blowup. It is something to do with PyTurboJPEG.

I downloaded and installed 2.0.3 libjpeg-turbo. The problem persists.

@lilohuang lilohuang self-assigned this Sep 23, 2019
lilohuang added a commit that referenced this issue Sep 23, 2019
Using __array_interface__ to get rid of memory growing reported by #12
@lilohuang
Copy link
Owner

The memory growing should be fixed. Please let me know if you still have question. Thank you.

@trdastidar
Copy link
Author

Yes, the problem has been addressed. Thanks for your support.

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