我在我的应用程序中使用自定义字体用于文本视图,当我使用这些字体时,我发现它消耗了大量内存(泄漏内存)。每次进入此活动时,堆大小都会增加,我使用此字体。当我从这个类中删除文本视图的自定义字体时,我注意到内存消耗减少了。请查看我的示例代码。我发布在
下面 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));
}
}
}
}
我在XML中使用此类,如下所示。
<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" />
另外,请在使用自定义视图时查看内存愚蠢。
** 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
我已将自定义字体放入资源中。谁能指出我哪里出错了。在这种情况下,任何帮助都非常感谢。
答案 0 :(得分:2)
使用自定义字体时这是一个非常常见的内存泄漏,我在此问题报告中有解决方案:https://code.google.com/p/android/issues/detail?id=9904