gpt4 book ai didi

C#读取XPS导致内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:24 25 4
gpt4 key购买 nike

我有一个只读取 XPS 文件的简单程序,我已阅读以下帖子,它确实解决了部分问题。
Opening XPS document in .Net causes a memory leak

class Program
{
static int intCounter = 0;
static object _intLock = new object();

static int getInt()
{
lock (_intLock)
{
return intCounter++;
}
}

static void Main(string[] args)
{

Console.ReadLine();

for (int i = 0; i < 100; i++)
{

Thread t = new Thread(() =>
{
var ogXps = File.ReadAllBytes(@"C:\Users\Nathan\Desktop\Objective.xps");
readXps(ogXps);
Console.WriteLine(getInt().ToString());
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
Thread.Sleep(50);
}
Console.ReadLine();
}


static void readXps(byte[] originalXPS)
{
try
{

MemoryStream inputStream = new MemoryStream(originalXPS);
string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
Uri packageUri = new Uri(memoryStreamUri);
Package oldPackage = Package.Open(inputStream);
PackageStore.AddPackage(packageUri, oldPackage);
XpsDocument xpsOld = new XpsDocument(oldPackage, CompressionOption.Normal, memoryStreamUri);
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();


//The following did solve some of the memory issue
//-----------------------------------------------
var docPager = seqOld.DocumentPaginator;
docPager.ComputePageCount();
for (int i = 0; i < docPager.PageCount; i++)
{
FixedPage fp = docPager.GetPage(i).Visual as FixedPage;
fp.UpdateLayout();
}
seqOld = null;
//-----------------------------------------------


xpsOld.Close();
oldPackage.Close();
oldPackage = null;
inputStream.Close();
inputStream.Dispose();
inputStream = null;
PackageStore.RemovePackage(packageUri);
}
catch (Exception e)
{

}
}
}

^ 该程序将读取 XPS 文件一百次 Before apply the fix^在应用修复之前

After the fix^应用修复后

所以帖子中的修复建议确实消除了一些对象,但是我发现仍然有像 DispatcherContextLayoutManagerMediaContext 这样的对象仍然存在于内存中并且它们的数量正好是 100,这是正常行为还是内存泄漏?我该如何解决?谢谢。

25/7/2018 更新
添加行 Dispatcher.CurrentDispatcher.InvokeShutdown(); 确实摆脱了 DispatcherContextLayoutManagerMediaContext 对象,不知道这是否是理想的修复方法。

最佳答案

看起来您剩下的那些类来自 XPSDocument,它们实现了 IDisposable,但您没有调用它们。还有一些实现相同接口(interface)的类,如果它们实现了,根据经验,要么将它们包装在 using 语句中,以保证它们的 Dispose方法被调用或自己调用他们的 Dispose 方法。

您的 readXps 方法的改进版本如下所示:

static void readXps(byte[] originalXPS)
{
try
{
using (MemoryStream inputStream = new MemoryStream(originalXPS))
{
string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
Uri packageUri = new Uri(memoryStreamUri);
using(Package oldPackage = Package.Open(inputStream))
{
PackageStore.AddPackage(packageUri, oldPackage);
using(XpsDocument xpsOld = new XpsDocument(oldPackage, CompressionOption.Normal, memoryStreamUri))
{
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();

//The following did solve some of the memory issue
//-----------------------------------------------
var docPager = seqOld.DocumentPaginator;
docPager.ComputePageCount();
for (int i = 0; i < docPager.PageCount; i++)
{
FixedPage fp = docPager.GetPage(i).Visual as FixedPage;
fp.UpdateLayout();
}
seqOld = null;
//-----------------------------------------------
} // disposes XpsDocument
} // dispose Package
PackageStore.RemovePackage(packageUri);
} // dispose MemoryStream
}
catch (Exception e)
{
// really do something here, at least:
Debug.WriteLine(e);
}
}

这至少应该清理大部分对象。我不确定您是否会在分析中看到效果,因为这取决于在您的分析过程中是否实际收集了对象。分析调试版本可能会产生意想不到的结果。

由于这些对象实例的其余部分似乎绑定(bind)到 System.Windows.Threading.Dispatcher 我建议您可以尝试保留对您的线程的引用(但此时您可能会考虑查看任务)ansd 一次线程完成,调用静态 ExitAllFrames在调度程序上。

您的主要方法将如下所示:

Console.ReadLine();

Thread[] all = new Thread[100];
for (int i = 0; i < all.Length; i++)
{
var t = new Thread(() =>
{
var ogXps = File.ReadAllBytes(@"C:\Users\Nathan\Desktop\Objective.xps");
readXps(ogXps);
Console.WriteLine(getInt().ToString());
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
all[i] = t; // keep reference
Thread.Sleep(50);
}
foreach(var t in all) t.Join(); // https://stackoverflow.com/questions/263116/c-waiting-for-all-threads-to-complete
all = null; // meh
Dispatcher.ExitAllFrames(); // https://stackoverflow.com/a/41953265/578411
Console.ReadLine();

关于C#读取XPS导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51463348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com