Skip to content

Conversation

shouze
Copy link

@shouze shouze commented Oct 24, 2018

Priorto that fix, when I was adding 100 times the same file into a zip,
I had this kind of memory consumption:

php zip.php
100/100 [============================] 100%  1 sec/1 sec  28.0 MiB

After the fix I obtain a constant memory consumption, whatever the
number of times I add the file:

php zip.php
100/100 [============================] 100%  1 sec/1 sec  4.0 MiB

Copy link
Collaborator

@NicolasCARPi NicolasCARPi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Thank you for your contribution.

Please refrain from using the variable name 'data', it is not descriptive enough. Can you change that?

For File.php you can return directly at line 464, no need for intermediate variable.

@shouze
Copy link
Author

shouze commented Oct 24, 2018

@NicolasCARPi done, so true about data 😉

@@ -325,8 +325,9 @@ public function addFileFromPsr7Stream(
public function finish(): void
{
// add trailing cdr file records
foreach ($this->files as $file) {
$file->addCdrFile();
foreach ($this->files as $data) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also remove "data" here please? foreach files as file conveys better the intent.

Priorto that fix, when I was adding 100 times the same file into a zip,
I had this kind of memory consumption:

```
php zip.php
100/100 [============================] 100%  1 sec/1 sec  28.0 MiB
````

After the fix I obtain a constant memory consumption, whatever the
number of times I add the file:

```
php zip.php
100/100 [============================] 100%  1 sec/1 sec  4.0 MiB
````
@shouze
Copy link
Author

shouze commented Oct 25, 2018

@NicolasCARPi done

@NicolasCARPi
Copy link
Collaborator

@shouze Could you provide the code you used to monitor the memory usage please? I want to reproduce.

@shouze
Copy link
Author

shouze commented Oct 25, 2018

@NicolasCARPi yes of course, here's my test script to reproduce:

zip.php

<?php
require 'vendor/autoload.php';

const ITERATIONS_COUNT = 100;
const ZIP_FILENAME = 'test.zip';

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

$fd = fopen(ZIP_FILENAME, 'w');
$opt = new ZipStream\Option\Archive();
$opt->setOutputStream($fd);
$zip = new ZipStream\ZipStream(ZIP_FILENAME, $opt);
$progressBar = new ProgressBar(new ConsoleOutput(), ITERATIONS_COUNT);
$progressBar->setFormat('debug');

for ($i = 1; $i <= ITERATIONS_COUNT; $i++) {
	$filename = sprintf('extracted/img-%0'.strlen(ITERATIONS_COUNT).'d.jpg', $i);
	$zip->addFileFromStream($filename, fopen('img.jpg', 'r'));
	$progressBar->advance();
}

$progressBar->finish();

$zip->finish();
fclose($fd);

composer.json

{
    "require": {
        "maennchen/zipstream-php": "dev-master",
        "symfony/console": "^4.1"
    },
    "scripts": {
	"test": [
		"rm -rf extracted",
		"php zip.php",
		"unzip test.zip",
		"ls -lha extracted/"
	]
    }
}

img.jpg

img

@NicolasCARPi
Copy link
Collaborator

Ok thank you. Looks good to me!

I'll let @maennchen merge it if he agrees.

@maennchen
Copy link
Owner

@NicolasCARPi Feel free to do so yourself. You‘ve examined the PR enough yourself.

@maennchen maennchen merged commit b8a89ef into maennchen:master Oct 27, 2018
@shouze shouze deleted the fix-memory-leak branch October 27, 2018 21:47
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

Successfully merging this pull request may close these issues.

3 participants