HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
MSHTML memory resources
Posted by: grin (---.wroclaw.cvx.ppp.tpnet.pl)
Date: Sunday, 20-Apr-2003, 08:22:38

Hi,
I have a problem with releasing memory resources occupied by MSHTML
objects (HTMLDocument), I've been pointed out that the HtmlEditor handles
clean up very well, I played with it and I found that it has basically the same
problem. The method HtmlEditor::CleanupControl() which is responsible for
releasing memory doesn't work. I added "Release" button to DemoHtmlApp.Form1 with this code atached:

this.htmlEditor1.CleanupControl();
// or this.htmlEditor1.Dispose();
this.htmlEditor1 = null;
System.GC.Collect();
this.Refresh();

and when I click on it the document disapears but memory resources
aren't released. So the question rises: how to release memory occupied
by MSHTML(before exiting the code)?

Re: MSHTML memory resources
Posted by: Tim Anderson (---.server.ntl.com)
Date: Sunday, 20-Apr-2003, 18:15:44

Well, it is a slippery subject.

I've tried an experiment. In this one, I display the htmlEditor in a modal dialog, within a loop that runs 1000 times. The code is like this (Vcool smiley:

Dim i As Integer
For i = 0 To 1000
Dim f As New Form2 ' form with htmlEditor
f.ShowDialog()
Me.Label1.Text = GC.GetTotalMemory(True)
f.Dispose()
GC.Collect()
Label2.Text = i.ToString ' just to check progress
Application.DoEvents()
Next i

Form2 has an event handler for ReadyStateChanged, looking for "complete". On the first "complete", it loads a document. On the 2nd "complete", it closes the dialog.

On first load, GC.GetTotalMemory returns 67788. Task Manager 8408.

With the test running, GC.GetTotalMemory goes up fairly quickly to 101300. However, it doesn't increase after that. Task Manager shows 15908K, again it doesn't increase.

After 1000 loops, the memory settles to 92120. Minimizing the app has the usual odd effect in task manager - mem down to 872K. However, GC.GetTotalMemory stays at 92120.

Now I try a similar test with a dialog containing a text control instead of htmlEditor. This time, the dialog loads a small piece of text, and then a timer closes the form. I get broadly the same result, except that the memory usage peaks at 75260 and stays there. Task manager 9452K.

I don't know quite how to interpret this, but it is a mildly encouraging result I think.

Tim

Re: MSHTML memory resources
Posted by: grin (---.wroclaw.cvx.ppp.tpnet.pl)
Date: Sunday, 20-Apr-2003, 21:19:13

Hi,

The problem isn't about many instances of HtmlEditor(), try this test:

1) Comment this line out from Form1.InitializeComponent(),
so there is no instance of HtmlEditor:

this.htmlEditor1 = new onlyconnect.HtmlEditor();

compile & run, check memory usage using Task Manager
- mine is showing 170448 K

2) Uncomment the line out, so there is only one instance of HtmlEditor,
compile & run, check memory usage
- mine is showing 182624 K

3) Add three more instances of HtmlEditor:
this.htmlEditor2 = new onlyconnect.HtmlEditor();
this.htmlEditor3 = new onlyconnect.HtmlEditor();
this.htmlEditor4 = new onlyconnect.HtmlEditor();

compile & run, memory usage is 182880 K
-----

As you can see the amount of memory used by one instance of HtmlEditor
is almost the same as amount used by four instances(it's about 12Mcool smiley,
with next instances memory is increasing but not so drastically as with first.
What I want to do is to remove at some point in my code one (and only)HtmlEditor instance, so I can reduce memory usage as in point 2)
to the level of point 1).

Re: MSHTML memory resources
Posted by: Tim Anderson (---.server.ntl.com)
Date: Monday, 21-Apr-2003, 06:08:31

Well, I can't get this behaviour even with a simple dialog containing just a text box. After I show, close and dispose the dialog, mem usage is higher than it was before. However it's not a leak as such, because it doesn't go on increasing.

Tim

Re: MSHTML memory resources
Posted by: grin (---.wroclaw.cvx.ppp.tpnet.pl)
Date: Monday, 21-Apr-2003, 10:49:01

This is not quite the same situation, let me explain.
The problem isn't about HtmlEditor or HtmlDocument instances
it's rather about core MSHTML library which is loaded just after
first "new HtmlEditor()" call and which occupies about 12MB.
I want to be able to unload the MSHTML library which seems to be a problem.

The example you gave above is about UI library which is a part of the Windows and .NET runtime. Ther's no way you can unload it (or load - it just stays in memory).

Re: MSHTML memory resources
Posted by: Tim Anderson (---.server.ntl.com)
Date: Monday, 21-Apr-2003, 12:51:10

Which brings me back to the earlier question - how are you tracking this memory?

GC.GetTotalMemory, which you mentioned, doesn't report it. If you display an htmlEditor with a loaded document GC.GetTotalMemory only reports 80K or so. I agree that Task Manager reports more than this, but that seems to be a different thing. You can reduce it by minimzing the app or by calling the SetProcessWorkingSetSize API.

I'm not saying you're wrong; but I'd like to know how to track it.

Tim

Re: MSHTML memory resources
Posted by: grin (---.wroclaw.cvx.ppp.tpnet.pl)
Date: Monday, 21-Apr-2003, 15:31:34


I'm tracking total system memory with Task Manager(last tab) I've just realized that you're probably using processes tab to track this memory.
I see your point now, minimizing the app reduces process memory, but
total system memory stays at the same level, how to interpret that?

I think that the GetTotalMemory() method only tracks memory resources
allocated from managed code(MSHTML is unmanaged),
so you're right that it doesn't report it. I didn't noticed that because I
was only tracking differences(mem_before - mem_after).

Re: MSHTML memory resources
Posted by: Tim Anderson (---.server.ntl.com)
Date: Monday, 21-Apr-2003, 17:03:45

I'll keep hitting at this; but in the meantime I want to thank you for pointing towards another leak in htmlEditor. I discovered that if you handle the HtmlEvent, then the form on which htmlEditor sits never gets disposed :-(. Fortunately I think I've found the fix - see separate post.

Tim

Re: MSHTML memory resources
Posted by: Tim Anderson (---.server.ntl.com)
Date: Monday, 21-Apr-2003, 21:47:12

Here's an interesting snippet from a Microsoft engineer which you can find through Google groups:

"This is by design in the CLR. A DLL (or an assembly to be more correct) can
only be unloaded when the appdomain that has loaded it goes away."

I wonder if this is the issue here? In which case there might be a solution via using a separate AppDomain (I have never tried this).

Tim

Re: MSHTML memory resources
Posted by: grin (---.wroclaw.cvx.ppp.tpnet.pl)
Date: Tuesday, 22-Apr-2003, 08:04:53

Thanks Tim, I didn't know that. It realy might be the issue here, MSHTML
is about 8MB which is close to my "lost 12MB". I'll dive into the subject
you pointed me out and see what can be done.



Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
keGbD
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.