C#内存问题在RichTextBox中加载RTF内容 [英] C# Memory problem loading RTF content in RichTextBox

查看:77
本文介绍了C#内存问题在RichTextBox中加载RTF内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员!

在基于C#WPF的应用程序中,我正在执行SQL查询以使用MemoryStream类将RTF内容加载到RichTextBox控件中.
通过单击按钮,将执行查询,并打开一个新窗口,显示从MySQL服务器获取的RichTextBox内容.

我注意到当查询执行并从数据库获取RTF内容时(我已经对其进行了压缩,并在读取时将其解压缩),我的应用程序的内存正在增加.即使我关闭RichTextBox所在的窗口,内存也无法标准化...它保持不变.任何想法可能是什么原因造成的?提前谢谢!

这是按钮的点击方法

Hello, programmers!

In my C# WPF based application I''m executing SQL query to load RTF content into RichTextBox control using MemoryStream class.

By clicking a button, the query is executed and it opens a new window showing the RichTextBox content got from MySQL server.

I notice when the query executes and gets the RTF content from DB (I''ve compressed it and decompress it when read) the memory of my app is increasing. Even though I close the window where the RichTextBox is, the memory doesn''t normalize... it stays the same. Any ideas what might be causing that? Thanks in advance!

Here is the button Click method

private void button_Click(object sender, RoutedEventArgs e)
{
command.CommandText = "select content from table where title = '" + title + "'";; connection.Open();
Reader = command.ExecuteReader();
String lesson = String.Empty;
while (Reader.Read())
{
   lesson = Reader.GetValue(0).ToString();
}
Reader.Dispose();
connection.Dispose();

ShowLesson window = new ShowLesson();

TextRange textRange = new TextRange(sw.lesson_content.Document.ContentStart, sw.lesson_content.Document.ContentEnd);
textRange.Text = lesson;

TextRange tr = new TextRange(window.lesson_content.Document.ContentStart, window.lesson_content.Document.ContentEnd);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Decompress(tr.Text))); // I compress the rtf content in DB, so here I decompress it in order to save space
tr.Load(ms, System.Windows.DataFormats.Rtf);
ms.Dispose();
window.Show();
}



如您所见,我已经使用了Reader.Dispose();.和connection.Dispose();

即使关闭ShowLesson窗口后,内存是否仍然没有释放?发生这种情况的原因是什么?预先感谢您的帮助!



As you can see I''ve used Reader.Dispose(); and connection.Dispose();

Even after closing ShowLesson window the memory still doesn''t free? What''s the reason for this to happen? Thanks in advance for any help!

推荐答案

不要使用任务管理器来查看您的应用程序正在使用多少内存.骗你了

它向您显示的是.NET CLR为应用程序保留的内存量,而不是实际使用的内存量.使用内存分析器或性能监视器以及.NET内存计数器来查看您的应用程序的实际使用情况.

保留内存是.NET CLR在其托管堆中拥有的内存,可用于您的应用程序从中分配对象.当您的应用释放对象时,内存将返回到托管堆,就Windows而言,不是释放".

内存管理由CLR自动处理,并且在Windows中可以很好地发挥作用.当Windows需要内存时,CLR非常乐意释放所有可能的内容并将其返回给Windows.
Don''t use Task Manager to see how much memory your app is using. It''s lying to you.

What it''s showing you is the amount of memory that the .NET CLR has RESERVED for your application, not how much it''s actually using. Use a memory profiler or Performance Monitor and the .NET Memory Counters to see your apps actual useage.

Reserved memory is the memory that the .NET CLR has in its managed heap, available for your application to allocate objects from. When your app frees an object, the memory is returned to the managed heap, NOT FREED as far as Windows is concerned.

Memory management is handled automatically by the CLR and plays very nicely with Windows. When Windows needs memory, the CLR is more than happy to free up whatever it can and return it back to windows.


这篇关于C#内存问题在RichTextBox中加载RTF内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆