vgrem / phpSPO

Microsoft 365 Library for PHP.
MIT License
355 stars 115 forks source link

Memory Leak while listing more than 1000 files #232

Open sermetege opened 3 years ago

sermetege commented 3 years ago

I get Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 528384 bytes) in /vendor/vgrem/php-spo/src/Runtime/ClientObjectCollection.php on line 75

when I try to iterate items that I retrieved.

If I remove $this->addChild($item); from ClientObjectCollection.pgp->getIterator() function I get this error

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 548864 bytes) in /vendor/vgrem/php-spo/src/Runtime/Http/Requests.php on line 23

public function getFiles($title, $directory) {
        $web = $this->context->getWeb();
        $lists = $web->getLists();
        $documents = $lists->getByTitle($title);
        $items = $documents->getItems()->expand('FieldValuesAsText');
        $this->context->load($items);
        $this->context->executeQuery();
        $files = array();
        foreach ($items as $item){
            if(is_object($item->FieldValuesAsText)) {
                $filepath = $item->FieldValuesAsText->FileRef;
            } else {
                $filepath = $item->FieldValuesAsText["FileRef"];
            }
            if(strpos($item->ContentTypeId, "0x0101") === 0 && ($directory !== NULL && strpos($filepath, $directory) === 0)){
                $files[] = $item;
            }
        }

        return $files;
 }
Is there any way to solve it?
sermetege commented 1 year ago

@vgrem Hello, I could manage it by using ini_set('memory_limit','2048M'); which is not maintainable for my case. Is there a better way to manage it for big lists?