Closed
Bug 654106
Opened 14 years ago
Closed 14 years ago
Setting innerHTML leaks an observer until the page is closed
Categories
(Core :: DOM: HTML Parser, defect)
Core
DOM: HTML Parser
Tracking
()
VERIFIED
FIXED
mozilla6
People
(Reporter: Hughman, Assigned: hsivonen)
References
Details
(Keywords: memory-leak, testcase)
Attachments
(4 files, 1 obsolete file)
(deleted),
text/html
|
Details | |
(deleted),
text/html
|
Details | |
(deleted),
application/x-bzip
|
Details | |
(deleted),
patch
|
bzbarsky
:
review+
johnath
:
approval-mozilla-aurora+
|
Details | Diff | Splinter Review |
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Build Identifier: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
This could be a cause for many other bugs in bug 640452.
In my findings, setting innerHTML on an element will extra memory to be used every time something is set to it. Even setting it to an empty string causes memory problems.
Using the almost minimal test case(s) that I will attach, I can get Firefox 4.0.1, Aurora 5.0a2 2011-05-01 and Nightly 6.0a1 2011-05-01 to use over 1GB of RAM in around 3 minutes. This memory will not be freed until the page is refreshed or tab closed. I can even navigate other pages in the same tab without a memory free occurring.
I have had javascript.options.mem.log enabled while testing and CC/GC have definitely been running without freeing the memory.
Reproducible: Always
Steps to Reproduce:
1. Start some version of Firefox in a new profile.
2. Run test case (note that the test case does not actually make anything appear to change).
3. Monitor the memory usage using whatever method you can think of (about:memory, task manager, etc).
4. Click on page when you wish for the memory usage to stop rising (this is an event listener I placed in there to see if the memory cleared when left alone, does not seem to affect results).
I am wondering if this is used in some addons and if so, is it causing the same leak? Since they likely would never close/refresh without the browser closing it could cause problems without even loading a web page.
Reporter | ||
Comment 1•14 years ago
|
||
Reporter | ||
Updated•14 years ago
|
Attachment #529472 -
Attachment mime type: text/plain → text/html
Comment 2•14 years ago
|
||
[Changed platform to x86; since WOW64 in user agent indicates 32bit build of Firefox (platform refers to browser build, not OS)].
Comment 3•14 years ago
|
||
OK, I can _definitely_ reproduce on the attached testcase.
Henri, any idea what's going on here?
roc, do we have a good way of dumping out the heap to see where the memory is used?
Updated•14 years ago
|
Status: UNCONFIRMED → NEW
Component: General → HTML: Parser
Ever confirmed: true
QA Contact: general → parser
Reporter | ||
Comment 4•14 years ago
|
||
Ed: I will remember that for next time.
Reporter | ||
Comment 5•14 years ago
|
||
Comment on attachment 529477 [details]
Less minimal testcase to show its not just document.body or empty string
That select box for text/html when adding an attachment keeps setting it as text/plain...
Attachment #529477 -
Attachment mime type: text/plain → text/html
Assignee | ||
Comment 6•14 years ago
|
||
(In reply to comment #0)
> This memory will not be freed until the page is
> refreshed or tab closed. I can even navigate other pages in the same tab
> without a memory free occurring.
Sure sounds like the cached fragment parsing holding onto something that it shouldn't hold onto.
(In reply to comment #3)
> Henri, any idea what's going on here?
Not right away, no.
http://mxr.mozilla.org/mozilla-central/source/parser/html/nsHtml5Parser.cpp#553 is supposed to drop what there is to drop at the end of each fragment parse.
Comment 7•14 years ago
|
||
This is a massif output I got from running iceweasel 4.0 (basically, the same as firefox 4.0) with a more or less fresh profile, starting directly on the less minimal testcase.
Comment 8•14 years ago
|
||
OK, well, the first thing that shows is that nsHtml5Parser::ParseHtml5Fragment calls nsContentSink::Init which calls nsScriptLoader::AddObserver (passing a shim that makes sure that the content sink is not holding a strong ref to the sink).
There is no corresponding RemoveObserver call, as far as I can see. So the script loader's observer array keeps growing at one word per innerHTML set (modulo whatever reallocation algorithm that code actually uses).
The page does 0xFFF sets per interval firing; that's every 10ms in Fx4 and every 4ms on trunk if the processor is keeping up. So we should expect about 3.3MB/s growth on 64-bit in Fx4 and about 9MB on trunk. That actually matches my numbers pretty closely.
Comment 9•14 years ago
|
||
Henri, the fragment parser doesn't even need to be a script loader observer, right?
Comment 10•14 years ago
|
||
With this change, memory usage on the testcase in this bug is stable for me.
Comment 11•14 years ago
|
||
/me is watching this bug with *intense* interest :)
Comment 12•14 years ago
|
||
Hughmann: BTW, thanks for a *wonderful* bug report and test case. We get so many vague leak reports -- "I browse for a while, close all my tabs, and Firefox still holds onto lots of memory" -- that specific reports like this are extremely valuable.
Comment 13•14 years ago
|
||
Yeah, I meant to say that, actually. This is by far the best "memory leaks" report I've seen, complete with steps to reproduce and a characterization of how long the memory leaks for. This made hunting this down much much easier!
FWIW this is good stuff but I don't think it explains bugs like 653817, where memory usage persists after closing all tabs that contained Web apps.
Comment 15•14 years ago
|
||
Indeed. There's not a "the leak"...
Reporter | ||
Comment 16•14 years ago
|
||
I was actually trying to make a memory rise/leak around bug 650350 comment 10
at the time without much success before finding that the line I was using for
clearing the page was eating memory... not helpful for that test.
I have just run the test in Firefox 3.6.14 where it does not cause any memory
rise. So its a regression of some sort for 4.0.
Comment 17•14 years ago
|
||
Yeah, this is specific to the HTML5 parser, which first shipped in 4.0.
Assignee | ||
Updated•14 years ago
|
Assignee: nobody → hsivonen
Status: NEW → ASSIGNED
Assignee | ||
Comment 18•14 years ago
|
||
(In reply to comment #9)
> Henri, the fragment parser doesn't even need to be a script loader observer,
> right?
Right, AFAICT.
(In reply to comment #12)
> Hughmann: BTW, thanks for a *wonderful* bug report and test case.
Indeed. Very useful. Thank you.
Attachment #529520 -
Attachment is obsolete: true
Assignee | ||
Updated•14 years ago
|
OS: Windows 7 → All
Hardware: x86_64 → All
Assignee | ||
Updated•14 years ago
|
Attachment #529685 -
Flags: review?(bzbarsky)
Comment 19•14 years ago
|
||
Comment on attachment 529685 [details] [diff] [review]
Move the fragment mode flag upwards, avoid running some nsContentSink initialization steps if it is set
>+ mCanInterruptParser = mFragmentMode ? PR_FALSE : sCanInterruptParser;
I'd prefer:
mCanInterruptParser = !mFragmentMode && sCanInterruptParser;
r=me with that.
Attachment #529685 -
Flags: review?(bzbarsky) → review+
Comment 20•14 years ago
|
||
This isn't FF5-specific, so minusing for tracking-firefox5, but once this lands and is happy on central, would be nice to see an approval nom for aurora
Assignee | ||
Comment 21•14 years ago
|
||
(In reply to comment #19)
> I'd prefer:
>
> mCanInterruptParser = !mFragmentMode && sCanInterruptParser;
>
> r=me with that.
Thanks. Landed with the change:
http://hg.mozilla.org/mozilla-central/rev/c3c4c902e9cd
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla6
Assignee | ||
Updated•14 years ago
|
status-firefox5:
--- → affected
Comment 22•14 years ago
|
||
Great work Hughmann, Henri and bz!
Updated•14 years ago
|
Comment on attachment 529685 [details] [diff] [review]
Move the fragment mode flag upwards, avoid running some nsContentSink initialization steps if it is set
I think we'll want this for 5.
Attachment #529685 -
Flags: approval-mozilla-aurora?
Updated•14 years ago
|
Attachment #529685 -
Flags: approval-mozilla-aurora? → approval-mozilla-aurora+
Reporter | ||
Comment 24•14 years ago
|
||
Should the mozilla-2.0 branch also be patched?
If there is going to be a security update for 4.0 before 5.0 then I think it would be a good idea.
Comment 25•14 years ago
|
||
Barring a zero-day there are no more security updates planned before 5.0.
Updated•14 years ago
|
tracking-firefox6:
? → ---
Assignee | ||
Comment 26•14 years ago
|
||
Landed in Aurora:
http://hg.mozilla.org/releases/mozilla-aurora/rev/e56b37ce413b
Comment 28•13 years ago
|
||
Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0
I've tried to verify whether this issue was fixed using the following steps:
1. Opened the attached testcase: "Simplest testcase that still...."
2. Opened a task manager and start observing memory and CPU consumption
What happened is that memory didn't increase. Is this the intended behavior for this patch?
Thanks!
Comment 29•13 years ago
|
||
George: yes. If you try Firefox 4 you should see the memory usage increase.
Comment 30•13 years ago
|
||
(In reply to comment #29)
> George: yes. If you try Firefox 4 you should see the memory usage increase.
Great! Considering your comment and the fact that this is no longer reproducible on Firefox 6.0b3 I am setting the status to Verified Fixed.
Thanks!
Status: RESOLVED → VERIFIED
You need to log in
before you can comment on or make changes to this bug.
Description
•