Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Memory Consumption to Read XMLs in PowerShell

Background

PowerShell has an useful function to read and parse XML files: [XML]. But reading XML files and (especially) building DOMs are memory consuming function.

Question

How much memory do you need to read XML files with [XML]?

Methods

See http://d.hatena.ne.jp/N_Nao/20111226/1324926173 for mem.sh.

$ cat xml.ps1
$XML = [XML](Get-Content "./test.xml")
$ ls -l test.xml; ./mem.sh pwsh xml.ps1
-rw-rw-r-- 1 satob satob 7756  420 01:04 test.xml
'pwsh xml.ps1' -> Max RSS = 84172 KB.
$ ls -l test.xml; ./mem.sh pwsh xml.ps1
-rw-rw-r-- 1 satob satob 84556  420 01:04 test.xml
'pwsh xml.ps1' -> Max RSS = 85500 KB.
$ ls -l test.xml; ./mem.sh pwsh xml.ps1
-rw-rw-r-- 1 satob satob 929356  420 01:05 test.xml
'pwsh xml.ps1' -> Max RSS = 118520 KB.
$ ls -l test.xml; ./mem.sh pwsh xml.ps1
-rw-rw-r-- 1 satob satob 6395116  420 01:06 test.xml
'pwsh xml.ps1' -> Max RSS = 368376 KB.

Result

# (A) XML Size[KB] (B) Memory Consumption [KB] (C) Delta from #1-(B) (C)/(A)
1 8 84,172 - -
2 85 85,500 1,328 15.6
3 930 118,520 34,348 36.9
4 6,400 118,520 284,204 44.4

Consideration

  • Memory consumption of [XML] is 20~40x as large as the target XML file.
  • Memory/XML size ratio (C)/(A) can be larger when the XML size is larget.