phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Memory Leak while listing more than 1000 files

Open sermetege opened this issue 3 years ago • 1 comments

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 avatar May 04 '21 16:05 sermetege

@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?

sermetege avatar Oct 31 '22 09:10 sermetege