簡體   English   中英

C#中引發了類型為'System.OutOfMemoryException'的異常

[英]Exception of type 'System.OutOfMemoryException' was thrown in C#

我正在使用xslt將xml轉換為另一個xml的應用程序。 一個小的示例數據可以正常工作,但是當它上線時,它會顯示類型為System.OutOfMemoryException的Exception。 產品xml文件的大小超過600 mb。 在Windows任務管理器上,它顯示當應用程序內存使用量超過1.6 GB時發生崩潰。 我試圖增加物理內存和虛擬內存的大小。 那沒有幫助。

有什么建議么?

C#:

// Create the XsltSettings object with document() enabled and script disabled.
    XsltSettings xsltsettings = new XsltSettings(true, false);
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(this.XsltFile, xsltsettings, new XmlUrlResolver());

    // Create the writer.
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "\t";
    settings.OmitXmlDeclaration = true;
    settings.ConformanceLevel = ConformanceLevel.Auto;
    settings.CloseOutput = false;

    TextWriter writer = new StreamWriter(OutFile.Replace(".xml", ".txt"));
    XmlReader reader = XmlReader.Create(InFile);
    reader.MoveToContent();
    // Execute the transformation.
    xslt.Transform(reader, null, writer);
    writer.Close();
    reader.Close();

xslt:

<xsl:stylesheet version="1.0"
                xmlns="http://www.ncr.com/rsd/WebOffice/ConfigurationManagement"
                xmlns:acm="http://www.ncr.com/rsd/WebOffice/ConfigurationManagement"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl acm">
  <xsl:output method="text" indent="no"/>

  <xsl:template match="/">
    <xsl:for-each select="/Data/Record">
      <xsl:variable  name="desc">
        <xsl:call-template name="StringReplace">
          <xsl:with-param name="text" select="desc" />
          <xsl:with-param name="replace" select='"&apos;"' />
          <xsl:with-param name="by" select='"&apos;&apos;"' />
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable  name="startdate">
        <xsl:call-template name="NumberofDays">
          <xsl:with-param name="text" select="sale_start_date"/>
        </xsl:call-template>
      </xsl:variable>  
      <xsl:variable  name="quotes">'</xsl:variable>

      <xsl:value-of select="concat('(',
        key,
        ',',
        $quotes,
        normalize-space(substring($desc, 2, string-length($desc) - 2)),
        $quotes,
        ',',       
        sale_price div 100,
        ',',
        sale_deal,
        ',',
        $startdate,
        ',',
        sale_start_time,
        ')')"/>
      <xsl:choose>
        <xsl:when test="position() = last()">;</xsl:when>
        <xsl:otherwise>,</xsl:otherwise>
      </xsl:choose>
      <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="StringReplace">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="StringReplace">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="NumberofDays">
    <xsl:param name="text"/>
    <xsl:variable name= "year" select="floor($text div 10000)"/>
    <xsl:variable name= "month" select="floor(($text mod 10000) div 100)"/>
    <xsl:variable name= "day"  select="floor(($text mod 10000) mod 100)"/>

    <xsl:choose>
      <xsl:when test="$month != 0 and $day !=0">
        <xsl:variable name= "yeardate">
          <xsl:call-template name="CalculateJulianDay">
            <xsl:with-param  name="year" select="$year"/>
            <xsl:with-param name="month" select="$month"/>
            <xsl:with-param name="day" select="$day"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name= "startdate">
          <xsl:call-template name="CalculateJulianDay">
            <xsl:with-param  name="year" select="$year"/>
            <xsl:with-param name="month" select="1"/>
            <xsl:with-param name="day" select="1"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="concat($year, format-number(number($yeardate)-number($startdate)+1,'000'))" ></xsl:value-of>
      </xsl:when>
      <xsl:otherwise>0000000</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="CalculateJulianDay">
    <xsl:param name="year"/>
    <xsl:param name="month"/>
    <xsl:param name="day"/>

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
    <xsl:variable name="y" select="$year + 4800 - $a"/>
    <xsl:variable name="m" select="$month + 12 * $a - 3"/>

    <xsl:value-of select="$day + floor((153 * $m + 2) div 5) + $y * 365 +
      floor($y div 4) - floor($y div 100) + floor($y div 400) -
      32045"/>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

xml:

<?xml version="1.0" encoding="us-ascii"?>
<Data>
    <Record>
        <key>00000000000017</key>       
        <desc>"P&amp;G FIXTURE"</desc>       
        <sale_price>120</sale_price>
        <sale_deal>0</sale_deal>
        <sale_start_date>20121002</sale_start_date>
        <sale_start_time>0001</sale_start_time>
    </Record>
</Data>

您可能會在32位系統上達到32位進程大小的2 Gb限制

如果您移至64位系統,但繼續使用32位進程,則您的應用大小限制為4Gb

在具有64位進程的64位系統上,您可以達到8Tb左右

不確切知道您的xslt對象是什么,而是猜測它是XslCompiledTransform。 您是否看到過: MSDN博客文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM