繁体   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