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,