New issue
Advanced search Search tips

Issue 825235 link

Starred by 1 user

Issue metadata

Status: Fixed
Owner:
Closed: Apr 2018
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 1
Type: Bug



Sign in to add a comment

Potential memory leak in printing::PdfMetafileSkia::RenderPage

Project Member Reported by etienneb@chromium.org, Mar 23 2018

Issue description

Out of Process Heap Profiling is a project that, for a very small set users, records heap allocations and sends back anonymized heap dumps. We've obtained a heap dump that suggests that there's a potential PdfMetafileSkia memory leak. We do not have repro steps. Small allocations [in size or frequency] were pruned from the heap dump before it was uploaded.

I've attached the symbolized trace. For instructions on how to use it, see https://chromium.googlesource.com/chromium/src/+/lkcr/docs/memory-infra/heap_profiler.md.

In this heap dump, there are 2450 calls to malloc from printing::PdfMetafileCg::RenderPage without corresponding calls to free.  These objects take up 642M + 642M (1.2G) of memory.

 
trace-0ecf74866d712b29.gz
437 KB Download
I attached the output of the memory dump diff tool (JSON formatted) and the two top potential memory leak stackframes.
bug1.png
87.3 KB View Download
bug2.png
85.0 KB View Download
process_487_malloc-leaks.json
895 KB View Download
As we can see in the trace, the malloc allocator is using 1.7G of live memory (still allocated).


overview.png
13.2 KB View Download
Cc: tdres...@chromium.org
Owner: thestig@chromium.org
Status: Assigned (was: Untriaged)
Looks like we aren't releasing a bunch of stuff.
For example, I think we need to CGPDFPageRelease after each CGPDFDocumentGetPage.

thestig@, does that sound right to you?
Components: Internals>Printing
Labels: M-67
Uhh, I'll double check the MacOS APIs. Good find.
Cc: thestig@chromium.org
Owner: etienneb@chromium.org
Punting this back since I didn't find anything wrong. Of course I could have easily missed something since I'm not a real Mac developer.

I looked over a bunch of example code and they don't release after calling CGPDFDocumentGetPage(). I tried calling CGPDFPageRelease() anyway and that causes a crash in printing_unittests with PdfMetafileCgTest.Pdf.

Do we have any idea if the data gets freed eventually after PdfMetafileCg gets destroyed?
The easiest way to figure out the leaked objects is to symbolize the missing frames - etienne - can you look into that?
I symbolized all the CoreGraphics frames from the top leaks JSON file. All of the leaks appear related to fonts in the PDF.

1) Are there example PDFs that have weird embedded fonts? 

2) How is this code path actually triggered? The magnitude and nature of the leak suggest that if we get the right PDF file, and trigger the logic, it should be pretty obvious to see the leak.
process_487_with_more_symbols.json
893 KB View Download
This code is triggered when printing to a printer or Preview.app. The PDFs are either generated by SkPDF, or are from the PDF Viewer.

PDFs can of course embed fonts, so potentially anything can be in there. PDFium has a test corpus and I have a stash somewhere, if you want to try just loading / printing them and see if any of them trigger leaks.
Labels: -M-67 M-68 ReleaseBlock-Beta
Owner: thestig@chromium.org
Great, I was able to repro this locally without any issue.

Repro steps:
1) Open any PDF
2) Open print page [cmd + P]
3) Select Open PDF in Preview
Repeat (3).

Eventually, close the browser window [but don't quite Chrome]. Note that the memory from (3) is still there.

With http://www.pdf995.com/samples/pdf.pdf, I was noticing an ~8MB leak per instance of (3).

I used Instruments to take an allocation trace, and saw the same stack that our in-the-wild reporter was showing. Over to thestig@ since we have a local repro. Marking as RB-B for M-68, since printing a large PDF will likely leak a large amount of memory, permanently.
Screen Shot 2018-03-29 at 3.06.14 PM.png
386 KB View Download
I found http://www.openradar.me/28415289 - could this be a bug in MacOS?
I haven't gone through the code too far in depth - I suspect that we're incorrectly using CoreGraphics. In the example you post, I suspect that if they made a new CG context and then freed it at the end, that they wouldn't leak resources. Ditto for our code.
Cc: ellyjo...@chromium.org
+ellyjones - this is a reproducible memory leak every time we print a PDF. It might be helpful if someone with macOS experience paired with thestig to investigate further.
ellyjones/thestig: Could this be prioritized?

A 1.2G leak seems like a significant issue, so please take a look when possible.
This may be related to  bug 260806 . Can you try applying this CL and see if the leak goes away? https://chromium-review.googlesource.com/c/chromium/src/+/987117

I tried running the browser on Mac with LSAN enabled, but the output was really noisy.
I will land my CL when the tree is open. Looking at bug 263359, which caused the  bug 260806  fix to be reverted, that was happening in an "ugly hack" for OS X 10.6. That code got removed in r385027, so my CL should be safe. If it is not and we get new crashes, we'll deal with them as they surface.
Owner: erikc...@chromium.org
erikchen: Can you check ToT and see if the leak has gone away? If so, mark as a duplicate of  bug 260806 .
Owner: thestig@chromium.org
I tried ToT with and without the CL in question - the CL helps a lot, but there's still a leak.

With the CL, the amount of leaked memory is down by 1 order of magnitude, from multiple MB per instance to 100s of KBs per instance. To repro:

1) Build Chrome locally
2) Attach Instruments - Allocator
3) Open PDF in print preview many times
4) Close the window.

Observe: multiple MBs leaked in CoreAnimation, as well as what appears to be leaked NSWindows. Ah ha!

Not sure if this is a leak in MacViews code or Print Preview code. Back over to thestig@, but Elly you may want to take a look at this as well.

Screen Shot 2018-04-09 at 3.55.23 PM.png
248 KB View Download
Screen Shot 2018-04-09 at 3.55.13 PM.png
215 KB View Download
The leak in comment 17 looks very different from the one in comment 9. I suspect this a MacViews leak, since the print preview code does not leak elsewhere AFAIK.
Status: Fixed (was: Assigned)
Alright - let's close out this bug since the main issue is fixed. I'll file a new bug with the MacViews bug.

Sign in to add a comment