PrintTicket DllHost.exe 内存爬升 [英] PrintTicket DllHost.exe Memory Climbs
问题描述
我正在使用 PrintTickets
打印大量 WPF 对象
,并且打印效果很好.我们确实遇到过这样一种情况,即程序使用了大量 dllhost.exe
内存并不断攀升,直到机器最终崩溃.我已经将范围缩小到创建PrintTicket
和printqueue.getprintcapabilitiesasxml()
.每次调用它们时,dllhost.exe
的内存使用量每次都会增加 3 MB 以上.谁能告诉我如果可能的话如何避免这种情况,或者在我知道它正在被使用后是否有办法清除它?
I'm using PrintTickets
to print a large number of WPF objects
and things are printing fine. We do have a situation that has been reported where the program is using a ton of the dllhost.exe
memory and climbs until the machine eventually crashes. I've narrowed it down to creating a PrintTicket
and the printqueue.getprintcapabilitiesasxml()
. Each time these are called it jumps the memory usage of the dllhost.exe
each time by sometimes 3+ MB. Can anyone tell me either how to avoid this if possible or if there is a way to clear it after i know it is being used?
更新:我用简单的代码创建了一个项目.
Update: I've created a project with simple this code.
for (int i = 0; i < 100; i++)
{
using (System.Printing.PrintQueue pQueuePrnt =
new LocalPrintServer().GetPrintQueue("HP LaserJet 9050 PCL 5"))
{
using (MemoryStream printerCapXmlStream = pQueuePrnt.GetPrintCapabilitiesAsXml())
{
Console.WriteLine(printerCapXmlStream.Length);
}
}
}
这将导致 dllhost.exe
中几乎 60 MB 永远不会消失.
This will result in almost 60 MB in the dllhost.exe
that will never go away.
推荐答案
每当您看到无法解释的内存增加时,您首先应该怀疑的是您的代码在某处泄漏了内存.检查您调用的所有函数,跟踪它们的返回值,并确保在完成它们后将它们全部处理掉.
Any time you see unexplained memory increases, the first thing you should suspect is that your code is leaking memory somewhere. Check all of the functions you call, trace their return values, and make sure that you are disposing of them all when you are finished with them.
您说您已将范围缩小到对PrintQueue.GetPrintCapabilitiesAsXml
的调用.这是一个好的开始.因此,让我们查看该方法的文档,看看它返回什么.啊,它回来了
You say that you've narrowed it down to the call to PrintQueue.GetPrintCapabilitiesAsXml
. That's a good start. So let's check the documentation for that method to see what it returns. Ah, it returns
A MemoryStream
指定打印机的功能
A
MemoryStream
specifying the printer's capabilities
所以现在我们需要找出 MemoryStream
到底是什么.特别是,我们需要知道它是否实现了IDisposable
interface 因此需要由我们的代码处理.根据文档,确实如此,因为MemoryStream
继承自 Stream
.
So now we need to find out what, exactly, a MemoryStream
is. In particular, we need to know if it implements the IDisposable
interface and therefore needs to be disposed by our code. According to the documentation, it does, because MemoryStream
inherits from Stream
.
据推测,您的代码正在检索所有这些新的 MemoryStream
对象,然后让它们泄漏其非托管资源.您需要为每个对象显式调用 Dispose
方法,或者将它们的检索和使用包装在 using
语句中;例如
Presumably, your code is retrieving all of these new MemoryStream
objects and then letting them leak their unmanaged resources. You need to either call the Dispose
method explicitly for each object, or wrap their retrieval and use in a using
statement; e.g.
using (MemoryStream ms = PrintQueue.GetPrintCapabilitiesAsXml())
{
ms.DoSomething();
// ...
} // the object is automatically disposed here
这篇关于PrintTicket DllHost.exe 内存爬升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!