2

I am using custom font for text views in my application and when i use these fonts, I found that it is consuming a large amount of memory(leaking memory). Heap size increases every time I enter into this activity, where I use this font. When I removed custom font for text view from this class, I noticed that memory consuption is reduced. Please have a look into my sample code. i am posting it below

 public class TrebuchetItalicTextView extends TextView {

    private static final String fontName = "Trebuchet-ms-italic";


    public TrebuchetItalicTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {

            synchronized (PSDataCache.getSharedCache().getFontCache()) {

                if (!PSDataCache.getSharedCache().getFontCache()
                        .containsKey(fontName)) {
                    Typeface tf = Typeface.createFromAsset(context.getAssets(),
                            "fonts/trebucit_0.ttf");
                    PSDataCache.getSharedCache().setFontCache(fontName, tf);
                }

                setTypeface(PSDataCache.getSharedCache().getFontCache()
                        .get(fontName));

            }

        }

    }

}

I use this class in XML as shown below.

<com.android.customviews.TrebuchetItalicTextView
                        android:id="@+id/text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="15dp"
                        android:text="Lorem ipsum dolor sit amet."
                        android:textColor="#727272"
                        android:textSize="16sp"
                        android:textStyle="italic" />

Also please a take a look on memory dumb, when custom views are used.

** MEMINFO in pid 10045 [com.android.test] **
                         Shared  Private     Heap     Heap     Heap
                   Pss    Dirty    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------
       Native        0        0        0    15596    14337     1258
       Dalvik    44483    11156    44292    64775    43074    21701
       Cursor        0        0        0                           
       Ashmem        0        0        0                           
    Other dev        4       36        0                           
     .so mmap     5380     2196     4752                           
    .jar mmap        0        0        0                           
    .apk mmap      362        0        0                           
    .ttf mmap      102        0        0                           
    .dex mmap      964        0        0                           
   Other mmap     1283      320      204                           
      Unknown     9452      528     9448                           
        TOTAL    62030    14236    58696    80371    57411    22959

 Objects
               Views:      981         ViewRootImpl:        2
         AppContexts:        4           Activities:        3
              Assets:       28        AssetManagers:       28
       Local Binders:       16        Proxy Binders:       19
    Death Recipients:        0
     OpenSSL Sockets:        1

 SQL
         MEMORY_USED:        0
  PAGECACHE_OVERFLOW:        0          MALLOC_SIZE:        0


 Asset Allocations
    zip:/data/app/com.android.test-2.apk:/resources.arsc: 135K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS.ttf: 133K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS.ttf: 133K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS.ttf: 133K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS.ttf: 133K
    zip:/data/app/com.android.test-2.apk:/assets/fonts/Trebuchet MS Bold.ttf: 123K

I have put the custom font in assets. Could anyone point out me where I went wrong. Any help in this case is highly appreciated.

aromero
  • 25,681
  • 6
  • 57
  • 79
Sanal Varghese
  • 1,465
  • 4
  • 23
  • 46
  • Well, you're adding the typeface to a cache. So yes, as long as the cache continues to exist and this font isn't kicked out of the cache, the font will continue to use memory. – Gabe Sechan Jun 01 '14 at 06:16
  • How much is a "large amount" of memory? – Karakuri Jun 01 '14 at 06:56
  • @Karakuri: Actually when custom font is not used, heap usage revoles around 60%- 64% and when it is used suddenly it shoots to 87%. Then the entire application becomes either slow or at times it crashes. Also it works well on high end devices like Nexus 7, Nexus 10 Inch etc. But when it comes to low end devices, this becomes a problem. – Sanal Varghese Jun 01 '14 at 09:49

1 Answers1

2

This a pretty common memory leak when using custom fonts, i have the solution on this issue report: https://code.google.com/p/android/issues/detail?id=9904

JavierSP1209
  • 899
  • 8
  • 17