1

I am developing a simple pdf reader rewriting an existing example. My app has a custom view with two buttons next and previous. When the next button is clicked it downloads a pdf from the internet and shows it in the view. Each time the next or previous button is clicked the following showPdf() method is called.

public void showPdf(){ RelativeLayout lout = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.main, null); setContentView( lout ); m_vPDF = null; m_vPDF = (PDFReader)lout.findViewById(R.id.PDFView); m_vPDF.open( m_doc); m_vPDF.setViewListener(m_vPDF); LinearLayout bar_find = (LinearLayout)lout.findViewById(R.id.bar_find); // btn_prev = (Button)bar_find.findViewById(R.id.btn_prev); btn_next = (Button)bar_find.findViewById(R.id.btn_next); // btn_prev.setOnClickListener(this); btn_next.setOnClickListener(this); } 

PDFReader is the object that has a viewer to show PDFs. Following is a part of the PDFReader code.

public class PDFReader extends View implements PDFView.PDFViewListener, ThumbView.ThumbListener { private PDFView m_viewer = null; public PDFReader(Context context) { super(context); } public PDFReader(Context context, AttributeSet attrs) { super(context, attrs); } public void set_viewer( int view_style ) { switch( view_style ) { case 1: m_viewer = new PDFViewHorz(); break; case 2: m_viewer = new PDFViewScroll(); break; case 3: m_viewer = new PDFViewSingle(); break; case 4: m_viewer = new PDFViewSingleEx(); break; case 5: m_viewer = new PDFViewReflow(); break; default: m_viewer = new PDFViewVert(); break; } if( m_viewer != null ) { if( doc != null ) m_viewer.viewOpen(getContext(), doc, 0xFFCC0000, 4); m_viewer.viewSetAnnotListener( annot_listener ); m_viewer.viewSetViewListener( view_listener ); m_viewer.viewResize(getWidth(), getHeight()); if( pos != null ) m_viewer.viewGoto(pos); } } 

In my showPdf I initialize PDFReader each time a button is clicked and inside PDFReader an instance for PDFView is initialized with PDFViewVert object and never garbage collected even if set the PDFReader to null and PDFView to null. As a result it is causing outofmemory error.

This is the memory analysis of dump which shows 14 instances of PDFViewVert have been created and never deallocated.

14 instances of "com.radaee.pdfex.PDFViewVert$1", loaded by "dalvik.system.PathClassLoader @ 0x412a0b08" occupy 19,602,792 (64.39%) bytes. Biggest instances: com.radaee.pdfex.PDFViewVert$1 @ 0x412a1ae8 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x412c7c30 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x418bdf78 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x41a382f8 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x41a4df50 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x41eaf1c0 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x4202a8e0 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x421aaa10 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x4233dbf8 - 1,543,040 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x412a0bf0 - 1,543,024 (5.07%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x412bbf58 - 1,390,792 (4.57%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x412f0790 - 1,390,792 (4.57%) bytes. com.radaee.pdfex.PDFViewVert$1 @ 0x424c4b50 - 1,390,792 (4.57%) bytes. Keywords com.radaee.pdfex.PDFViewVert$1 dalvik.system.PathClassLoader @ 0x412a0b08 

Any help is appreciated.. Thanks,

1
  • Is my answer helps you to solve your problem? Commented Dec 12, 2012 at 7:19

3 Answers 3

2

Java GC does not necessarily clean objects that are not referenced any more. It can decide to do nothing with them if there is enough memory so far.

The real check of potential memory leak can be done with memory profiler only.

But you can try easier check. Try to decrease heap size of your application and call System.gc(). This does not guarantees to trigger GC, however if it starts removing your objects it is a good sign that you probably do not have memory leak.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried the System.gc() and it is not triggered. The heap size I have is 40 mb and after 20-23 instances of PDFView are created I get the outofmemory error. Is there any specific point in the code where I need to call System.gc()?
1

Try with this :

Runtime.getRuntime().gc(); 

Change this

m_vPDF = null; 

To,

if (m_vPDF != null){ m_vPDF.recycle(); m_vPDF = null; } 

This will helps you to clean unused objects.

Hope it helps you.

Thanks.

2 Comments

when you are done with your instances of PDFViewVert you can call Runtime.getRuntime().gc();.
also follow the way to make instance null, in my edited solution.
0

Personally I think your best option is to use a custom Adapter instead, and that way it will reuse the views, and you wont get 23 instances of the pdfview. It will only save the one, and it will make you able to swipe instead of hitting next or previous. Unfortunately these devices dont have alot of memory you make due with what you got

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.