Is it important to close PdfRenderer and when

534 views Asked by At

I am using PdfRenderer in my app. In this app a user can open a PDF file and browse it. He can open another PDF file in the same session.

I just noticed, that I don't close it at all after use or before opening a new PDF. I just do the following:

mPdfRenderer = new PdfRenderer(mFileDescriptor);

In the Google example though the PdfRenderer is always closed after use.

if (null != mCurrentPage) 
  mCurrentPage.close();
mPdfRenderer.close();
mFileDescriptor.close();

Is it important to call close() before opening a new PDF?

1

There are 1 answers

0
Ali On

NOTE: This is a general answer will cover all the scenarios including yours

Well in general practice, you need to free up resources that are bound to your task, to be more precise Non-preemptive resources, it is considered to be best practice but, to remove the worker thread from the pool isn't extremely crucial nowadays, In short, for your answer

  1. If you want to employ best practices you can do that
  2. If you don't then when the application closes Garbage Cleaner will do your task for you when there will be a need of resources, or when the cleanup is scheduled

When to Clean Resources or Close worker Threads

  1. When you want to close the application
  2. When you need other tasks and you know that that task will take up a lot of resources and to make your application efficient you really wanted to do that.
  3. When you want to Leave your application onPause(), your application for a moment (considered to be best practice but most developers don't do that, because some times, those process may take more resources on restarting because of a bundle processing[many processes may start on onStart() ])

for your question's answer, I hope the above statement might clarify everything.