Bug 692278

Summary: memory leaks in mupdf for android
Product: MuPDF Reporter: palmicq
Component: mupdfAssignee: Robin Watts <robin.watts>
Status: RESOLVED FIXED    
Severity: normal CC: abun.santo, jackie.rosen, palmicq, robin.watts, vk872903, W90043, yultae
Priority: P4    
Version: unspecified   
Hardware: Other   
OS: other   
Customer: Word Size: ---

Description palmicq 2011-06-13 15:44:26 UTC
I opened a large pdf file (30MB) and continuous clicked the next page button, the memory usage of mupdf keeps increasing until be killed by android system.

It seems that the function Java_com_artifex_mupdf_MuPDFCore_gotoPageInternal in android/jni/mupdf.c has memory leaking. I commented out Java_com_artifex_mupdf_MuPDFCore_drawPage function body and the memory still leaks. I tried commented out rest parts after error = pdf_load_page(&currentPage, xref, pagenum); statement in Java_com_artifex_mupdf_MuPDFCore_gotoPageInternal function, the memory stop leaking. I think maybe it was caused by currentPageList, however I added the following codes at the end of this function:

if (currentPageList != NULL) {
	LOGD("fz_free_display_list");
		fz_free_display_list(currentPageList);
		currentPageList = NULL;
}

it doesn't take effect and the memory still keep leaking.
Comment 1 Robin Watts 2011-07-04 15:32:29 UTC
I can't see any obvious memory leaks (although, that's not to say there aren't any).

If I download the image into the emulator (with pdf_reference17.pdf as the test file) and start it up, then do:

  adb shell dumpsys meminfo com.artifex.mupdf

It tells me that I have a total of 17240K allocated after the first page is displayed.

If I then step forward (click next repeatedly) to page 100, the same thing tells me that I have 20544K allocated. Return to page 1, and I have 21000K. If I repeat this again and again, I asymptotically reach 21338K or so.

We expect memory to increase for every *new* page that is displayed, as mupdf has to hold the positions/lengths etc of all the different stream objects used in each new page. This explains why the memory use climbs as we move forward to page 100. Fragmentation explains why we see the memory usage climb a bit even when we tread backwards over pages we have seen before.

On the whole it looks like we aren't losing memory purely by changing from one page to another.

I think there *may* be a memory leak to do with exiting the app and then restarting it as the total memory seems to jump significantly every time I exit and restart.
Comment 2 Robin Watts 2011-07-04 16:47:37 UTC
I've fixed the 'restarting app causing large leak' problem. A patch should be working its way towards the main repo as we speak.

If this doesn't solve the problem, please reopen the bug with some more details, including exactly the file you are using and step by step instructions allowing me to reproduce what you are doing here. (In particular how you are measuring the memory usage).

Thanks.
Comment 3 palmicq 2011-07-06 10:55:23 UTC
Hi Robin,

Should colorspace needs to be freed in Java_com_artifex_mupdf_MuPDFCore_destroying(JNIEnv * env, jobject thiz)?

like this:

fz_drop_colorspace(colorspace);
colorspace = NULL;

Thanks.
Comment 4 Robin Watts 2011-07-06 11:54:53 UTC
(In reply to comment #3)
> Should colorspace needs to be freed in
> Java_com_artifex_mupdf_MuPDFCore_destroying(JNIEnv * env, jobject thiz)?

No, because that's a pointer to a statically allocated thing.
Comment 5 Kim, Yultae 2011-07-12 04:58:04 UTC
Comparing to the pdfapp.c, two lines are added.

   currentRotate = currentPage->rotate; 
   currentPage->links = NULL;  // added
    ..
   fz_free_device(dev);
   pdf_age_store(xref->store, 3); // added
   
Memory leaks looks to be gone.

May help.
Comment 6 William Lee 2011-07-12 15:15:58 UTC
(In reply to comment #5)
> Comparing to the pdfapp.c, two lines are added.
> 
>    currentRotate = currentPage->rotate; 
>    currentPage->links = NULL;  // added
>     ..
>    fz_free_device(dev);
>    pdf_age_store(xref->store, 3); // added
> 
> Memory leaks looks to be gone.
> 
> May help.

Can you specify which portion in pdfapp.c needs to be change? I can not find any place in pdfapp.c saying currentRotate = currentPage->rotate.
Comment 7 palmicq 2011-07-12 18:53:05 UTC
(In reply to comment #5)
> Comparing to the pdfapp.c, two lines are added.
> 
>    currentRotate = currentPage->rotate; 
>    currentPage->links = NULL;  // added
>     ..
>    fz_free_device(dev);
>    pdf_age_store(xref->store, 3); // added
> 
> Memory leaks looks to be gone.
> 
> May help.

Cool! The cache limitation function works for me. Thank you!
Comment 8 William Lee 2011-07-12 23:39:12 UTC
(In reply to comment #6)
> (In reply to comment #5)
> > Comparing to the pdfapp.c, two lines are added.
> > 
> >    currentRotate = currentPage->rotate; 
> >    currentPage->links = NULL;  // added
> >     ..
> >    fz_free_device(dev);
> >    pdf_age_store(xref->store, 3); // added
> > 
> > Memory leaks looks to be gone.
> > 
> > May help.
> 
> Can you specify which portion in pdfapp.c needs to be change? I can not find
> any place in pdfapp.c saying currentRotate = currentPage->rotate.

I add those two lines but the app still crashes. I open a 300 Mb pdf and can only access first 2 pages
Comment 9 Santo Soetarmin 2011-09-07 08:50:47 UTC
Hi palmicq@gmail.com do you check your Android memory when loading pdf?
I'm using the patch in #comment 6, still having problem in the #comment 1.

Hi William Lee, do your issue solved?
Thanks
Comment 10 Santo Soetarmin 2011-09-07 08:51:16 UTC
Hi palmicq@gmail.com do you check your Android memory when loading pdf?
I'm using the patch in #comment 6, still having problem in the #comment 1.

Hi William Lee, do your issue solved?
Thanks
Comment 11 Vinay Kumar 2012-10-16 11:05:47 UTC
(In reply to comment #7)
> (In reply to comment #5)
> > Comparing to the pdfapp.c, two lines are added.
> > 
> >    currentRotate = currentPage->rotate; 
> >    currentPage->links = NULL;  // added
> >     ..
> >    fz_free_device(dev);
> >    pdf_age_store(xref->store, 3); // added
> > 
> > Memory leaks looks to be gone.
> > 
> > May help.
> 
> Cool! The cache limitation function works for me. Thank you!

Hi may I know the file name where should I add this code to. Or could you please send me the updated file.

Thank you.