Androidにおけるアプリケーションdrawableピクチャリソースのメモリ使用量の統計

5471 ワード

実際のプロジェクトでは、アプリケーションのdrawableの下にある画像やテキストがロードされている場合、最大どれだけのメモリが消費され、定性的に分析されるかを統計する必要があります. 
Androidにおける画像占有メモリの計算については、「Androidにおける画像占有メモリの計算」を参照してください.
以下に簡単な統計コードを示します.
static
void
test
(
Context
context
){
   
final
String
TAG
=
"robin"
;
   
long
m
=
0
;
   
long
n
=
0
;
   
Class
c
=
R
.
drawable
.
class
;
   
Field
f
[]=
c
.
getFields
();
   
int
id
=
0
;
   
int
w
=
0
;
   
int
h
=
0
;
   
Resources
res
=
context
.
getResources
();
   
for
(
int
i
=
0
;
i
<
f
.
length
;
i
++){
       
try
{
            id
=
f
[
i
].
getInt
(
null
);
       
}
catch
(
IllegalArgumentException
e
)
{
           
            e
.
printStackTrace
();
       
}
catch
(
IllegalAccessException
e
)
{
           
            e
.
printStackTrace
();
       
}
       
Drawable
d
=
res
.
getDrawable
(
id
);
       
if
(
d
instanceof
BitmapDrawable
){
           
Bitmap
b
=((
BitmapDrawable
)
d
).
getBitmap
();
            w
=
b
.
getWidth
();
            h
=
b
.
getHeight
();
            n
=
w
*
h
;
           
Log
.
i
(
TAG
,
"the size of "
+
f
[
i
].
getName
()+
"is "
+
w
+
"X"
+
h
+
"pixels"
);
            m
=
m
+
n
;
       
}
   
}
   
Log
.
i
(
TAG
,
"the area of all bitemap is "
+
m
);
    m
=
m
<<
1
;
   
Log
.
w
(
TAG
,
"in Config.RGB_565,the memory occupied by bitemap:"
+(
m
>>
20
)+
"M"
+((
m
&((
1
<<
20
)-
1
))>>
10
)+
"K"
);
    m
=
m
<<
1
;
   
Log
.
w
(
TAG
,
"in Config.ARGB_8888,the memory occupied by bitemap:"
+(
m
>>
20
)+
"M"
+((
m
&((
1
<<
20
)-
1
))>>
10
)+
"K"
);
    c
=
R
.
string
.
class
;
    f
=
c
.
getFields
();
   
String
str
=
null
;
    m
=
0
;
   
for
(
int
i
=
0
;
i
<
f
.
length
;
i
++){
       
try
{
            id
=
f
[
i
].
getInt
(
null
);
       
}
catch
(
IllegalArgumentException
e
)
{
           
            e
.
printStackTrace
();
       
}
catch
(
IllegalAccessException
e
)
{
           
            e
.
printStackTrace
();
       
}
        str
=
res
.
getString
(
id
);
        m
=
m
+
str
.
length
()*
2
;
   
}
   
Log
.
i
(
TAG
,
"string:"
+(
m
>>
20
)+
"M"
+((
m
&((
1
<<
20
)-
1
))>>
10
)+
"K"
+(
m
&((
1
<<
10
)-
1
)));
}
実行結果:
I/robin   (21677): the size of app_detail_bottom_menu_divider is 3X5 pixels
I/robin   (21677): the size of cancel is 51X52 pixels
I/robin   (21677): the size of confirm is 51X52 pixels
I/robin   (21677): the size of ic_launcher is 96X96 pixels
I/robin   (21677): the size of item1 is 640X1067 pixels
I/robin   (21677): the size of item2 is 2560X1600 pixels
I/robin   (21677): the size of item3 is 2560X1600 pixels
I/robin   (21677): the size of item4 is 2560X1600 pixels
I/robin   (21677): the size of item5 is 2560X1600 pixels
I/robin   (21677): the size of item6 is 2560X1600 pixels
I/robin   (21677): the size of item7 is 2560X1600 pixels
I/robin   (21677): the size of splash is 640X1067 pixels
I/robin   (21677): the area of all bitemap is 25956295
W/robin   (21677): in Config.RGB_565,the memory occupied by bitemap:49M519K
W/robin   (21677): in Config.ARGB_8888,the memory occupied by bitemap:99M15K
I/robin   (21677): string:0M0K432
終わりだ!