Powershell 创建多达 50k xml 文件最终导致内存泄漏

问题描述 投票:0回答:1

我必须循环遍历包含最多 58k 行的 csv 文件。每行有 60 个分号分隔的值。 我必须为每一行创建一个 xml 文件。我的脚本正在执行我期望的操作,但创建 39k 文件需要 20 个小时,而且运行速度非常慢,因为它消耗了所有系统内存。 总共有 10 个 csv 文件,行数在 30k 到 90k 之间。对于每一行,我都必须创建一个 xml 文件。

我怎样才能避免这种内存消耗,有更好的方法来实现这一点吗?

我正在使用 Import-csv 读取 csv 数据,循环查找文件中的每个 ID 这是我的代码:

函数 createxmlfromcsvcharge{

$csv = Import-csv $conf_YearFiles$conf_File -Delimiter ";" 
    
foreach($ChargingID in $CSV){

    $ID = $ChargingID.ChargingID
    
    #file name 
    $FileName = "MIS_Record_2013_"+$ID+".xml"
    
    # Creating a simple XML document
    $xmlDocument = New-Object System.Xml.XmlDocument
    
    $dec = ($xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", $null))
    $root = $xmlDocument.DocumentElement
    $xmlDocument.InsertBefore($dec,$root)
            
    # Adding a root element
    $rootElement = $xmlDocument.CreateElement("ChargingRecord")
    $xmlDocument.AppendChild($rootElement)
    
    # create <ChargingID> container node
    $xmluser = $xmlDocument.CreateElement('Charge')
    $rootElement.AppendChild($xmluser)
    
    # add each property from csv entry to <ChargingID>
    Foreach ($property in $csv[0].psobject.Properties.name) {
        # create a property node
        $xmlproperty = $xmlDocument.CreateElement($property)
        $text = $xmlDocument.CreateTextNode($ChargingID.$property)      
        $xmlproperty.AppendChild($text)
        # add to current <ChargingID> node
        $xmluser.AppendChild($xmlproperty)
    }
    
    $xmlDocument.Save('m:\temp\2013\'+$FileName)
}

}

代码完全生成了我所期望的。

非常感谢任何帮助或提示。谢谢。

xml powershell memory
1个回答
0
投票

它需要大量的时间和内存,因为它正在管理文件和相关对象的全部重量。

只需将

Import-Csv
管道连接到
Foreach-Object
就可以改善情况:

$DestinationDirectory = 'm:\temp\2013\' 

Import-Csv $conf_YearFiles$conf_File -Delimiter ';' |
    ForEach-Object {
        $ID = $PSItem.ChargingID
        
        #file name 
        $FileName = Join-Path -Path $DestinationDirectory -ChildPath "MIS_Record_2013_${ID}.xml"
        
        # Creating a simple XML document
        $xmlDocument = New-Object System.Xml.XmlDocument
        
        $dec = ($xmlDocument.CreateXmlDeclaration('1.0', 'UTF-8', $null))
        $root = $xmlDocument.DocumentElement
        $xmlDocument.InsertBefore($dec, $root)
                
        # Adding a root element
        $rootElement = $xmlDocument.CreateElement('ChargingRecord')
        $xmlDocument.AppendChild($rootElement)
        
        # create <ChargingID> container node
        $xmluser = $xmlDocument.CreateElement('Charge')
        $rootElement.AppendChild($xmluser)
        
        # add each property from csv entry to <ChargingID>
        Foreach ($property in $PSItem.psobject.Properties.name) {
            # create a property node
            $xmlproperty = $xmlDocument.CreateElement($property)
            $text = $xmlDocument.CreateTextNode($PSItem.$property)      
            $xmlproperty.AppendChild($text)
            # add to current <ChargingID> node
            $xmluser.AppendChild($xmlproperty)
        }
        
        $xmlDocument.Save( $FileName)
    }
© www.soinside.com 2019 - 2024. All rights reserved.