1- طبق گفته Sun، از دستورات System.runFinalizersOnExit()
و Runtime.runFinalizersOnExit()
استفاده نکنید، اینها منسوخ و Unsafe اعلام شدن:
JAVA-DOC: Because it is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. While this problem could be prevented if the class whose objects are being finalized were coded to "defend against" this call, most programmers do not defend against it. They assume that an object is dead at the time that its finalizer is called. Further, the call is not "thread-safe" in the sense that it sets a VM-global flag. This forces every class with a finalizer to defend against the finalization of live objects!
Joshua Bloch: Never call System.runFinalizersOnExit or Runtime.runFinalizersOnExit for any reason: they are among the most dangerous methods in the Java libraries.
2- Handler یا Timer؟ (در شرایط کاربری یکسان)
Handler: براحتی reschedule میشه، هر اینترفیس Runnable
ای رو میشه داخلش attach کرد، حافظه کمتری رو مصرف می کنه، بهترین گزینه برای update های UI هستش، از Multi-threading پشتیبانی می کنه، استثناهای پرتاب شده از task ها و thread هارو هندل میکنه، در ساخت Thread
میشه Handler اون thread رو هم باهاش طراحی کرد، زمانی که دستگاه به حالت sleep بره کماکان به کارش ادامه میده!
Timer: قابلیت reschedule شدن نداره، فقط یک TimerTask
رو میشه داخلش attach کرد، حافظه بیشتری رو اشغال میکنه، بهترین گزینه برای background-task ها هستش، نمی تونه UI رو update کنه، فقط با یک Thread
کار میکنه(بقیه task ها باید queue بمونن تا کار اولی تموم بشه)، استثناهای task و thread رو هندل نمی کنه و فقط نابودشون میکنه(دیگر task ها رو هم اجرا نخواهد کرد)، زمانی که دستگاه به حالت sleep بره متوقف میشه.
3- آیا میدونید با فعال کردن خاصیت android:debuggable="true"
در فایل AndroidManifest.xml پروسه Proguard غیرفعال میشه؟
4- اگر به استثنا نوع UnknownHostException
برخورد کردید و نتونستید این نوع استثنا رو catch
کنید، تعجب نکنید. چراکه استثنای UnknownHostException
از دسته java.net هستش درحالی که Exception
از دسته java.lang!
راه ساده:
try { // Codes. } catch (final java.net.UnknownHostException exception) // java.net { // Handling codes. } catch (final java.lang.Exception exception) // java.lang { // Handling codes. }
5- با تغییر color scheme تصاویر به RGB_565 می تونید میزان حافظه اشغالی توسط تصویر رو کاهش بدید:
البته فقط کمی کیفیت تصویر پایین میاد...
BitmapFactory.Options _options = new BitmapFactory.Options(); _options.inPreferredConfig = Config.RGB_565;