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.