-
Notifications
You must be signed in to change notification settings - Fork 23
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
ctypes.POINTER memory leak in _buffer_reader
/ _buffer_writer
#346
Comments
That's crazy, As for |
Well, we could do addr = ctypes.cast(p_buf_first, ctypes.c_void_p).value
buf = (ctypes.c_char * size).from_address(addr) but this is a bit sketchy as it mixes up pointer and address. Pointer values are usually memory addresses, but they don't necessarily have to be according to the C spec. Yet, in practice, we could probably assume they are. |
Ah wait, how about |
BTW, this is not only Edit: Previously, this comment stated that |
|
Yeah, I agree that calling a private method to clear the cache is nasty. We still get dynamic class construction:
Perhaps those are GC'd correctly though. I'll test once a new release is out. Thanks for a fast turn-around on this. |
Well, that's just the ctypes way of creating arrays: https://docs.python.org/3/library/ctypes.html#arrays Footnotes
|
Checklist
pypdfium2
fromPyPI
orGitHub/pypdfium2-team
.Description
In the callbacks for reading and writing PDF files,
ctypes.POINTER
objects are being created at runtime:This is problematic since pointer types are cached, and since
size
can vary in the last chunk, there's an unbounded growth of the type cache.This leads to a 500Mb process eventually growing to 2.6Gb after ~8hrs of read/write operations on PDFs.
Running
tracemalloc.compare_to
confirms that this is where all the memory is going.A workaround is to call
ctypes._reset_cache()
after closing the PDF (or after context__exit__
), but that seems like a bit of a hack.I'm not really familiar with the CFFI, so not sure what the canonical way of reading/writing buffers is given a pointer and a size, but it seems like there should be another way which doesn't require declaring pointer types whenever you read a chunk into a buffer. No idea.
Either way, memory is stable if you do a
ctypes._reset_cache()
when releasing PDF resources after I/O operations.Install Info
The text was updated successfully, but these errors were encountered: