本文介绍了Visuals的批量打印到Xps导致OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我以这种方式打印到xps: PrintQueue队列= printQueueObject作为PrintQueue;if(队列!= null){ queue.Refresh(); collat​​or = PrintQueue.CreateXpsDocumentWriter(queue).CreateVisualsCollat​​or(); collat​​or.BeginBatchWrite(); ... collat​​or.Write(visual); collat​​or.EndBatchWrite();} 大约打印1000页后,出现OutOfMemoryException.使用内存探查器后,问题根源似乎是以下链: InterleavingNode -> System.Windows.Xps.Packaging.XpsFixedPageReaderWriter -> _parentNode(字段)-> XpsFixedDocumentReaderWriter -> _pageCache(类型为List< IXpsFixedPageReader>的字段) ---> XpsFixedPageReaderWriter的800个实例 -----> System.IO.StringWriter的2400实例= System.Text.StringBuilder的2400实例或换句话说:在我们生成XPS文档时,似乎页面缓存一直在增长.因此,如果我们生成包含数千个页面的巨大文档,则迟早会出现OutOfMemoryException(取决于用户的可用内存).这里也有类似的问题: https://connect.microsoft.com/VisualStudio/feedback/details/788507/creating-xps-document-with-visuals-collat​​or-consumes-a-very-large-amount-of-memory 但是从来没有针对此的解决方案或解决方法.我当前正在使用Windows 8.谢谢解决方案 Hi,I am printing to xps in this way:PrintQueue queue = printQueueObject as PrintQueue;if (queue != null){ queue.Refresh(); collator = PrintQueue.CreateXpsDocumentWriter(queue).CreateVisualsCollator(); collator.BeginBatchWrite(); ... collator.Write(visual); collator.EndBatchWrite();}After 1000 pages or so are printed, I get an OutOfMemoryException. After using a memory profiler, the problem source seems to be the following chain:InterleavingNode-> System.Windows.Xps.Packaging.XpsFixedPageReaderWriter-> _parentNode (field) --> XpsFixedDocumentReaderWriter--> _pageCache (field of type List<IXpsFixedPageReader>)---> 800 instances of XpsFixedPageReaderWriter-----> 2400 instances of System.IO.StringWriter = 2400 instances of System.Text.StringBuilderor in other words:There seems to be a page cache that is growing all the time, while we are generating the XPS document. So if we generate a huge document with many thousands of pages we get sooner or later an OutOfMemoryException (depending on the users available memory).There has been a similar question here:https://connect.microsoft.com/VisualStudio/feedback/details/788507/creating-xps-document-with-visuals-collator-consumes-a-very-large-amount-of-memoryBut there has never been a solution or workaround to this. I am currently using Windows 8.Thanks 解决方案 这篇关于Visuals的批量打印到Xps导致OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 08:44