問題描述
本機堆不斷增加 (Native heap keeps increasing)
First I'm using API 10.
I have activity A and activity B.
I launch activity B from A. Then I call finish() in the oncreate() of B. I then launch activity B from A again and so on. When i do this my native heap keeps increasing by about .5 MB every time.
This is the only code I have in activity B.
super.onCreate(savedInstanceState);
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
logHeap(); //keeps track of native heap size
setContentView(R.layout.gameplay);
finish();
return;
Are the bitmaps in layout not getting recycled? I don't understand why my native heap size would keep increasing.
參考解法
方法 1:
You should make sure you're calling bitmap.recycle() when you no longer need the Bitmap. It may take some time for the Bitmap to be recycled by GC.
方法 2:
Found most of it.
Apparently creating from assets
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Capture_it.ttf");
isn't garbage collected.
Removing it seemed to fix most of the problem.