Use large memory objects in PowerShell
A brief introduction to the purpose of large memory objects in PowerShell, and the way to open the PowerShell super-large Memory object.
PowerShell Missionary original article 2016-12-31 permitted reprint, but must retain the name and provenance, otherwise investigate legal liability
PowerShell 2GB large object. NET Large Objects
Preface
Q: In PowerShell, which objects are likely to consume large memory?
Answer: StringBuilder object, Large array arraylist,hashset and so on.
Q: What is the use of Stringbuildr objects in PowerShell?
A: It's more common to have large files look for replacements in memory, which is a lot faster than string.
Q: How much text can I save with a 1GB StringBuilder object?
A:. NET uses UTF16, that is, whether an English letter or a Chinese character, takes up 2 bytes. So say 1GB text files, read into memory, probably to occupy about 2.1GB of memory.
Body
Question: A variable takes up 1GB of memory, is it big?
I have a test machine, with the WIN10 LTSB 2016 system, physical memory is still 30G, virtual memory 20G. But creating a 1GB object is an error. and the establishment of 900MB is not an error, the code is as follows:
[System.reflection.assembly]::loadwithpartialname ("system.text") | Out-Null [int32]$long =$ Memory String Object = New-object System.Text.StringBuilder ($ Long)
Originally the manual said, the default can use 2GB of memory. Alas, this year everything shrinks, it seems I can only enable. NET super-large objects.
Premise:
1 with 64-bit system, enough physical memory, enough virtual memory.
2. NET version is at least 4.51, preferably the latest version of 4.62.
3 Specific solutions:
With Notepad, create a new text file,
File name: "Powershell.exe.config"
The file is encoded as: "Utf8+bom header", which is saved as UTF8 encoding with Notepad.
The contents of the file are:
<? ?><configuration> < runtime > <enabled= "true"/> </ runtime></configuration>
After saving, paste this file into "c:\Windows\System32\WindowsPowerShell\v1.0"
Re-run Powershell.exe, rerun the above script code, will not error.
Use large memory objects in PowerShell