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

lessen memory usage #335

Closed
wants to merge 1 commit into from
Closed

lessen memory usage #335

wants to merge 1 commit into from

Conversation

JO0st
Copy link

@JO0st JO0st commented Dec 5, 2018

apperently file_put_contents uses more memory than the seperate actions it is executing according to http://php.net/manual/en/function.file-put-contents.php

@akerouanton
Copy link
Contributor

Hello @JO0st,

Thank you for your PR. I checked the doc for file_put_contents but I can't see any claim about higher memory usage. Could you cite the section claiming that please?

Also, is this PR related to the issue you opened (#334)?

@JO0st
Copy link
Author

JO0st commented Dec 7, 2018

@NiR- It is related to the issue I made. Unfortunatly I don't have any evidence for the increased memory usage. I only noticed it on my installation: when using the file_put_contents operation it was impossible to generate a large pdf because of memory errors. When using the debugger the error always happened on the file_put_contents instruction. When i replaced it with the three seperate instructions everything worked as it should.

The idea of changing the instruction into three different ones came from a stackoverflow question

@fbourigault
Copy link
Contributor

According to https://blackfire.io/profiles/compare/78c2f364-0fe7-4e18-85c6-2ae517293d50/graph using file_put_contents or fwrite use the same amount of memory. The file is 636MB large.

Profiled script

<?php

$data = file_get_contents('input.txt');

function write($data) {
//    file_put_contents('output.txt', $data);
    $handle = fopen('output.txt', 'w');
    fwrite($handle, $data);
    fclose($handle);
}

write($data);

@akerouanton
Copy link
Contributor

Thanks @fbourigault, that's what I was looking for :)

Some more questions @JO0st, what's the source of the file content? Is it a stream? Because actually it feels weird that file_put_contents generate a memory allocation.

@JO0st
Copy link
Author

JO0st commented Dec 10, 2018

The data input is a string built in memory. The method gets called by snappy's generateFromHtml

@akerouanton
Copy link
Contributor

@JO0st Could you provide a full reproducer or at least the error message you got and your current snappy version please?

@JO0st
Copy link
Author

JO0st commented Dec 12, 2018

The error message is Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 33096176 bytes) in /var/www/html/sites/all/modules/civicrm/packages/snappy/src/Knp/Snappy/AbstractGenerator.php on line 317.

It gets created from from civicrm(https://github.com/civicrm/civicrm-core/blob/10bc9d11f88e9d7d5e321b9500013b2269fe9bf4/CRM/Utils/PDF/Utils.php#L234) .

This is the string that gets fed into snappy. I stripped all personal content from it https://nextcloud.fock.be/index.php/s/tmoXwzd4a9Dtis6. The file is too big to attach it to github.

@fbourigault
Copy link
Contributor

The file you uploaded is 31.5MB and the size PHP is trying to allocate is also 31.5MB (33096176 / 1024 / 1024). I would say it's related to loading the html content into memory.

* @param array $options

The line 317 is a PHPDoc comment. So which version of snappy is used?

@JO0st
Copy link
Author

JO0st commented Dec 12, 2018

i'm using the version supplied with civicrm. Unfortunatly they haven't documented which version they are using (https://github.com/civicrm/civicrm-packages/blob/faa4b03933b4d47a9a1098271b64405c81f4700e/VERSIONS.php#L110). However it looks like the snappy version in civicrm hasn't been updated in 6 years :( https://github.com/civicrm/civicrm-packages/tree/master/snappy

@stale
Copy link

stale bot commented Nov 12, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 12, 2019
@stale stale bot closed this Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants