powershell: [BUG] Get-PnPListItem with PageSize not releasing / reusing memory
Notice
Many bugs reported are actually related to the PnP Framework which is used behind the scenes. Consider carefully where to report an issue:
- Are you using
Invoke-PnPSiteTemplateorGet-PnPSiteTemplate? The issue is most likely related to the Provisioning Engine. The Provisioning engine is not located in the PowerShell repo. Please report the issue here: https://github.com/pnp/pnpframework/issues. - Is the issue related to the cmdlet itself, its parameters, the syntax, or do you suspect it is the code of the cmdlet that is causing the issue? Then please continue reporting the issue in this repo.
- If you think that the functionality might be related to the underlying libraries that the cmdlet is calling (We realize that might be difficult to determine), please first double check the code of the cmdlet, which can be found here: https://github.com/pnp/powershell/tree/master/src/Commands. If related to the cmdlet, continue reporting the issue here, otherwise report the issue at https://github.com/pnp/pnpframework/issues
Reporting an Issue or Missing Feature
Working with a tenant with MILLIONS of files that need to be copied to NTFS and then kept updated as they transition FROM O365 SharePoint.
I have a separate script for seeding data that walks the file folder structure of their libraries and copies (Get-PnPFile) the data to the NTFS target. It works, but it’s slow. To keep the NTFS target current I need to quickly run through ALL the files and add new or updated files. I am able to use Get-PnPListItem as a means to grab 1000’s of files / folders and quickly dispatch those that are unchanged.
The issue is with every page processed (I’m pulling 5000 items) approximately 128MB of memory is consumed and only a small fraction released before the next page is ‘loaded’. By the time I’ve hit 500,000 items, memory consumption is at 12-13GB. Memory consumption occurs with each Page pulled (breakpoint at the script block verifies this).
I’ve included [System.GC]::GetTotalMemory($true) | out-null in the Function called ProcessItems (so once per 5000 items processed), but it doesn’t seem to make any significant difference
Expected behavior
Get-PnPListItem should release or reuse memory allocated for each Page loaded.
Actual behavior
Memory consumption per 5000 item Page of approximately 90-128 MB. Only a portion is released. By the time 500,000 items have been pulled system memory usage is a 12-13GB,
Steps to reproduce behavior
Get-PnPListItem -List $listUrl -PageSize $PageSize -ScriptBlock {Param($items) ShowProgress $items; ProcessItems $items -ErrorAction Continue; $items.Context.ExecuteQuery()}) | Out-Null
What is the version of the Cmdlet module you are running?
PnP.PowerShell v1.9.0 PowerShell v5.1
Which operating system/environment are you running PnP PowerShell on?
- [ X] Windows (Server 2016)
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17
My suggestion would be to use CAML query here. Since you have like a million items, maybe you can create query to retrieve items in chunks.
Something like this, pseudo code:
Get-PnPListItem -List $listUrl -Query "ID > 1 and ID < 10000" -PageSize $PageSize -ScriptBlock {Param($items) ShowProgress $items; ProcessItems $items -ErrorAction Continue; $items.Context.ExecuteQuery()}) | Out-NullDivide 1 million into chunks of say 20000 and then for every 20k items, execute the above query, with ID > 1 and ID < 20K and then ID > 20k and ID <40k and so on. Hope that helps.
Bueller?