How to handle Android loaded font memory leaks

Source: Internet
Author: User

During the development of the Android app, some external fonts are used. External fonts are prone to memory leaks when loaded.

Like what:

Typeface tf=Typeface.createfromasset (Getassets (), Consts.lanting_font_path); Title.settypeface (TF);

If you write this way every time you call, it will reload the font every time it executes, causing the memory to grow.

Run Monkey test time, execute:

adb shell  dumpsys meminfo "Packgename"  

This command will play the current application memory consumption, as follows:

* * Meminfo in PID 26390 [Packgemane] * *PSS Private Private swapped heap heap total Dirty clean Dirty Size Alloc Free------   ------   ------   ------   ------   ------   ------Native Heap0 0 0 0 24372 23593 758Dalvik Heap30256 30020 0 0 39116 34608 4508Dalvik Other2254 2180 0 0Stack132 132 0 0Cursor8 8 0 0Other Dev4 0 4 0. So mmap998 816 12 0. apk mmap1795 0 1636 0. TTF Mmap115 0 76 0. Dex Mmap1215 56 980 0Other mmap406 4 88 0Unknown22295 22292 0 0 Total59478 55508 2796 0 63488 58201 5266Objects Views:840 Viewrootimpl:7appcontexts:6 Activities:4Assets:assetmanagers:39Local Binders:Proxy binders:29Death Recipients:0OpenSSL Sockets:0SQL memory_used:296Pagecache_overflow:136 malloc_size:62DATABASES pgsz Dbsz lookaside (b) Cache Dbname4 28/19/5/xxxxxx4 9/18/4/xxxxxxAsset Allocations Zip:/system/app/xxxxx.apk:/assets/fonts/roboto-light.ttf:366k Zip:/system/app/xxxxx.apk:/assets/fonts/lantinghei.ttf:2344k Zip:/system/app/xxxxx.apk:/assets/fonts/coresansm25extralight.ttf:210k Zip:/system/app/xxxxx.apk:/assets/fonts/coresansm25extralight.ttf:210k .....Zip:/system/app/xxxxx.apk:/assets/fonts/coresansm25extralight.ttf:210k

From the log can be seen coresansm25extralight this font has been loaded several times, causing memory leaks.

Like and solve font memory leaks? The method is simple:

Defines a font as a constant and is initialized only in the OnCreate method.

Consts.tf_coresans_font  =  typeface.createfromasset (getassets (), Consts.coresans_font_path);

How to handle Android loaded font memory leaks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.