1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import
java.lang.ref.PhantomReference;
import
java.lang.ref.Reference;
import
java.lang.ref.ReferenceQueue;
import
java.lang.reflect.Field;
public
class
Test {
public
static
boolean isRun =
true
;
public
static
void
main(
String
[] args) throws Exception {
String
abc =
new
String
(
"abc"
);
System.out.println(abc.getClass() +
"@"
+ abc.hashCode());
final
ReferenceQueue referenceQueue =
new
ReferenceQueue<
String
>();
new
Thread() {
public
void
run() {
while
(isRun) {
Object
o = referenceQueue.poll();
if
(o !=
null
) {
try
{
Field rereferent = Reference.
class
.getDeclaredField(
"referent"
);
rereferent.setAccessible(
true
);
Object
result = rereferent.
get
(o);
System.out.println(
"gc will collect:"
+ result.getClass() +
"@"
+ result.hashCode());
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
}
}.start();
PhantomReference<
String
> abcWeakRef =
new
PhantomReference<
String
>(abc,
referenceQueue);
abc =
null
;
Thread.currentThread().sleep(
3000
);
System.gc();
Thread.currentThread().sleep(
3000
);
isRun =
false
;
}
}
|
1
2
|
class
java.lang.
String
@
96354
gc will collect:
class
java.lang.
String
@
96354
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@SuppressWarnings(
"unused"
)
private
Bitmap copressImage(
String
imgPath){
File picture =
new
File(imgPath);
Options bitmapFactoryOptions =
new
BitmapFactory.Options();
//下面这个设置是将图片边界不可调节变为可调节
bitmapFactoryOptions.inJustDecodeBounds =
true
;
bitmapFactoryOptions.inSampleSize =
2
;
int
outWidth = bitmapFactoryOptions.outWidth;
int
outHeight = bitmapFactoryOptions.outHeight;
bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
bitmapFactoryOptions);
float imagew =
150
;
float imageh =
150
;
int
yRatio = (
int
) Math.ceil(bitmapFactoryOptions.outHeight
/ imageh);
int
xRatio = (
int
) Math
.ceil(bitmapFactoryOptions.outWidth / imagew);
if
(yRatio >
1
|| xRatio >
1
) {
if
(yRatio > xRatio) {
bitmapFactoryOptions.inSampleSize = yRatio;
}
else
{
bitmapFactoryOptions.inSampleSize = xRatio;
}
}
bitmapFactoryOptions.inJustDecodeBounds =
false
;
//false --- allowing the caller to query the bitmap without having to allocate the memory for its pixels.
bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
bitmapFactoryOptions);
if
(bmap !=
null
){
//ivwCouponImage.setImageBitmap(bmap);
return
bmap;
}
return
null
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/* 自定义Adapter中部分代码*/
public
View getView(
int
position, View convertView, ViewGroup parent) {
File file =
new
File(it.
get
(position));
SoftReference<Bitmap> srf = imageCache.
get
(file.getName());
Bitmap bit = srf.
get
();
ImageView i =
new
ImageView(mContext);
i.setImageBitmap(bit);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(
new
Gallery.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT));
return
i;
}
|
1
2
3
4
5
6
7
8
9
|
InputStream
is
=
this
.getResources().openRawResource(R.drawable.pic1);
BitmapFactory.Options options=
new
BitmapFactory.Options();
options.inJustDecodeBounds =
false
;
options.inSampleSize =
10
;
//width,hight设为原来的十分一
Bitmap btp =BitmapFactory.decodeStream(
is
,
null
,options);
if
(!bmp.isRecycle() ){
bmp.recycle()
//回收图片所占的内存
system.gc()
//提醒系统及时回收
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 以最省内存的方式读取本地资源的图片
* */
public
static
Bitmap readBitMap(Context context,
int
resId){
BitmapFactory.Options opt =
new
BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable =
true
;
opt.inInputShareable =
true
;
//获取资源图片
InputStream
is
= context.getResources().openRawResource(resId);
return
BitmapFactory.decodeStream(
is
,
null
,opt);
}
|
1
2
|
if
(bitmapObject.isRecycled()==
false
)
//如果没有回收
bitmapObject.recycle();
|
1
2
3
4
|
private
final
static
floatTARGET_HEAP_UTILIZATION =
0
.75f;
//在程序onCreate时就可以调用
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
即可
|
1
2
3
|
private
final
static
int
CWJ_HEAP_SIZE =
6
*
1024
*
1024
;
//设置最小heap内存为6MB大小
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
|