-
-
Notifications
You must be signed in to change notification settings - Fork 173
Memory for fetched message attachments is never released which results in memory leak #531
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
Comments
I have the same issue! |
@robkleinee @veroca88 I also have the problem that Webklex uses a lot of memory. I have created an IMAP account for a test. It contains 7 e-mails, 3 of which have attachments with a total size of 20 MB (1x 10MB, 2x 5MB). This is enough to exceed a memory limit of 128 MB in PHP.
This returns 134MB for Memory consumption only seems to be a problem with (larger) attachments, because the data is stored multiple times in memory (for example as property $raw and $content). I have found two approaches to massively reduce memory consumption. At the end of the constructor of the Attachment class, I discard the local property $message and $part:
I know that this is not "clean" and can cause further problems. At least in my case it doesn't cause any problems. With this adjustment I get only 68MB for Furthermore, I do not retrieve messages all at once, but iterate over the messages with the following code:
This results in 13MB for I have no idea how best to simplify all this, but the library definitely needs to be customized and can no longer hold such large amounts of data in memory. And it's hard to apply the patches on my own (it requires extending a lot of classes). |
Hi @wurst-hans thanks for investigating and sharing what you've found. If you would like to tackle the core issue, please feel free to do so, even if it involves changes to several classes. I believe the benefits would be rather substantial - perhaps something for a major version jump to v7? |
I've been trying to understand the code better for a few hours now and I'm making progress, but slowly. I don't have much time at the moment so I'm focusing on optimizing my problems. I am very grateful for your library and of course try to give something back to the community. However, my customizations are very experimental and I still don't understand PHP IMAP well enough. The recommended customization of #531 does more than I thought. However, it makes a decisive difference whether you retrieve all messages at once with
or whether you only fetch the headers and retrieve each message individually with
This reduces memory consumption considerably. You only have to empty the attachments once after each access to an e-mail so that the memory consumption remains low. My suggestion is to check whether all properties/methods are really being used. I could imagine that you don't really need a Of course it's nice to have as much raw data as possible on the objects, but not in the case of large attachments (or hundreds of emails with small attachments, which in total also leads to problems). If I come up with something better, I'll get back to you. |
@wurst-hans Thanks for your investigation. I think there are two seperate problems concerning memory usage:
I opened this issue because of problem number 2. I think that's a bigger problem than problem number 1. The workaround I suggested in my openings post is working for me. It's running for months now in a production environment, without crashes. I think the problem has something to do with a circular dependency between some attachment objects, so the garbage collector can't destroy them when the message is destroyed. But at the moment I don't have the time to dive into it. Some more information about the workaround:
So in total (I also editted my openings post):
|
Thanks for clarification. I still prefer using I agree, that this might have to do with some circular references not being cleaned up. Nevertheless, the message data is kept at least 4 times in memory (sometimes as raw data, sometimes as already decoded data) for every message. But, yes, problem number two might have higher criticality. And I still haven't found the reason for that, really crazy. |
I think the most simple (lame?) solution could be to make a destructor on 'Message' class and clear the attachments of the message. That should fix the memory leak I think. |
A long-running script which fetches and processes messages with attachments from a mailbox with this IMAP package results eventually in a 'memory size of ... bytes exhausted'.
I think this has something to do with the fetched attachments. It seems that these attachments somehow are never released from memory?
A workaround is to set manually the attachments to an empty collection after the attachments are processed.
Code to duplicate the bug (be sure to have emails with attachments in the mailbox)
Package version: 5.5.0
PHP: 8.1.x
Workaround (Extended on 2025-05-08)
Am I doing something wrong? Or is there a memory leak?
Thanks in advance!
PS: It seems like this package is abandoned?
The text was updated successfully, but these errors were encountered: