简体   繁体   English

为什么Word2013中的InlineShape.add会导致内存泄漏

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

My program must add photos to the Word Document table. 我的程序必须将照片添加到Word文档表。 Faced with the fact that in Word 2013 the program works much slower or even hangs up. 面对这样一个事实:在Word 2013中,程序工作得慢得多,甚至挂起。 I began to look for problem areas and it seems that the problem is in the memory occupied when adding images. 我开始寻找问题区域,似乎问题在于添加图像时占用的内存。

I add images to Word 2013 table by InlinesShapes.AddPicture method. 我通过InlinesShapes.AddPicture方法将图像添加到Word 2013表。 after that, the used memory increases by ~ 50 Mb for each image. 之后,每个图像使用的内存增加~50 Mb。 When it reaches ~600 mb it stops growing at this rate and the growth is about 1 mb. 当它达到~600 mb时,它以此速率停止生长,并且增长约为1 mb。 Then at some point Word clears memory up to 200 and all over again. 然后在某些时候Word清除内存高达200并重新开始。

But if i do the same in Word 2007 the amount of siezed memory less than 1 kb, alltime. 但是,如果我在Word 2007中做同样的事情,那么simzed内存的数量不到1 kb。

Word 2007 on my Notebook (Win 8.1, i5-4300u), Word 2013 install on other machine (Virtual Desktop with Win 7, Xeon E5) - if it matter. 我的笔记本上的Word 2007(Win 8.1,i5-4300u),Word 2013安装在其他机器上(Win 7,Xeon E5的虚拟桌面) - 如果重要的话。

I'm adding the usual photos taken with the camera, for example: 4160x3120, 72 dpi, 24 bit, 500 Kb size, JPG. 我正在添加用相机拍摄的常用照片,例如:4160x3120,72 dpi,24 bit,500 Kb尺寸,JPG。

That sample code with used memory calculation 该示例代码使用了内存计算

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

Output by Word2013 由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

Output by Word2007 由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

There is code of MemCheck. 有MemCheck的代码。 Declared mobjSWbemServices as global, to exclude it from the problem. 将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

So what could be the problem? 那可能是什么问题呢? Am I doing something wrong or is it a bug and I need to install some updates? 我做错了什么或是一个错误,我需要安装一些更新?

There was a major change in the graphics engine used between Word 2007 (and 2010) and Word 2013 (and later versions). Word 2007(和2010)和Word 2013(以及更高版本)之间使用的图形引擎发生了重大变化。 That could be a factor. 可能是一个因素。 It's also a fact that the object model is slower in Word 2013 (and later), partly for security reasons, as I recall. 事实上,对象模型在Word 2013(及更高版本)中速度较慢,部分出于安全原因,我记得。 So there may not be a lot you can do. 所以可能没有太多可以做的事情。

One thing you can try is to clear Word's UNDO stack and explicitly SAVE the document. 您可以尝试的一件事是清除Word的UNDO堆栈并明确保存文档。 This will reduce the number of objects Word is managing in order to support Undo and could speed things up. 这将减少Word管理的对象数量,以支持撤消并可以加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM