簡體   English   中英

為什么Word2013中的InlineShape.add會導致內存泄漏

[英]Why InlineShape.add in Word2013 leads to memory leak

我的程序必須將照片添加到Word文檔表。 面對這樣一個事實:在Word 2013中,程序工作得慢得多,甚至掛起。 我開始尋找問題區域,似乎問題在於添加圖像時占用的內存。

我通過InlinesShapes.AddPicture方法將圖像添加到Word 2013表。 之后,每個圖像使用的內存增加~50 Mb。 當它達到~600 mb時,它以此速率停止生長,並且增長約為1 mb。 然后在某些時候Word清除內存高達200並重新開始。

但是,如果我在Word 2007中做同樣的事情,那么simzed內存的數量不到1 kb。

我的筆記本上的Word 2007(Win 8.1,i5-4300u),Word 2013安裝在其他機器上(Win 7,Xeon E5的虛擬桌面) - 如果重要的話。

我正在添加用相機拍攝的常用照片,例如:4160x3120,72 dpi,24 bit,500 Kb尺寸,JPG。

該示例代碼使用了內存計算

Sub A()  
    ' >-- SOME CODE --<

    Dim objCell As Word.Cell
    'user method that return first free Word.Cell from table
    Set objCell = TableManager.Get_FreeCell(objTable) 
    'clear
    Tools.MemCheck "<- mem before paste"

    objCell.Range.InlineShapes.AddPicture sFilePath 

    'clear
    Tools.MemCheck "-> mem after paste"

    ' >-- SOME CODE --<

    Set objCell = Nothing
 End Sub

由Word2013輸出

    296.156 Mb <- mem before paste
    345.793 Mb -> mem after Paste
    346.504 Mb <- mem before paste
    396.18  Mb -> mem after Paste

由Word2007輸出

    109.867 Mb <- mem before paste
    109.871 Mb -> mem after Paste
    109.871 Mb <- mem before paste
    109.879 Mb -> mem after Paste
    109.887 Mb <- mem before paste
    109.887 Mb -> mem after Paste

有MemCheck的代碼。 將mobjSWbemServices聲明為全局,以將其排除在問題之外。

Dim mobjSWbemServices as Object
Sub MemCheck(Optional ByVal sDescription As String = vbNullString)
    If mobjSWbemServices is nothing then Set mobjSWbemServices = GetObject("winmgmts:")
    Dim GetMemUsage As Double
     GetMemUsage = mobjSWbemServices.Get( _
        "Win32_Process.Handle='" & _
        GetCurrentProcessId & "'").WorkingSetSize / 1024 / 1024    

    Debug.Print Round(GetMemUsage, 3); " Mb"; vbTab; sDescription
   'GetCurrentProcessId  is Declared Library: 
     'Private Declare PtrSafe Function GetCurrentProcessId Lib "kernel32" () As Long
     'Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
End Sub

那可能是什么問題呢? 我做錯了什么或是一個錯誤,我需要安裝一些更新?

Word 2007(和2010)和Word 2013(以及更高版本)之間使用的圖形引擎發生了重大變化。 可能是一個因素。 事實上,對象模型在Word 2013(及更高版本)中速度較慢,部分出於安全原因,我記得。 所以可能沒有太多可以做的事情。

您可以嘗試的一件事是清除Word的UNDO堆棧並明確保存文檔。 這將減少Word管理的對象數量,以支持撤消並可以加快速度。

暫無
暫無

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

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