释放内存的麻烦。重新创建一个活动〜30次后我的内存不足

时间:2014-04-28 04:03:31

标签: android android-activity out-of-memory

活动即时通讯"重新创建()"在它的xml布局中包含一个surfaceview,看起来我很难从surfaceview使用的bitmapfactory释放内存。

每次重新创建时,每个解码10个位图,大约5-10KB。

我尝试将所有位图设置为null并在每次娱乐之前运行system.gc,但这并没有帮助。

我只是想定期重新创建我的表面视图,但我找不到比重新创建它更好的方法。

是否有一种更有效的方法来重新初始化我的表面视图,如果没有,你能告诉我这个logcat的垃圾收集是怎么回事吗?

04-28 03:39:05.368: E/AndroidRuntime(1939): FATAL EXCEPTION: main
04-28 03:39:05.368: E/AndroidRuntime(1939): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.irksomesloth.botbreaker/com.irksomesloth.botbreaker.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.os.Looper.loop(Looper.java:137)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Method.invoke(Method.java:525)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at dalvik.system.NativeStart.main(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Activity.setContentView(Activity.java:1895)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.irksomesloth.botbreaker.MainActivity.onCreate(MainActivity.java:15)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Activity.performCreate(Activity.java:5133)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-28 03:39:05.368: E/AndroidRuntime(1939):     ... 12 more
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: java.lang.reflect.InvocationTargetException
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-28 03:39:05.368: E/AndroidRuntime(1939):     ... 23 more
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: java.lang.OutOfMemoryError
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:379)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:409)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.irksomesloth.botbreaker.GameView.<init>(GameView.java:30)

1 个答案:

答案 0 :(得分:0)

查看位图recycle()函数。您可能还需要将位图选项设置为可清除。这是一个片段,我在其中调整位图大小并输出为JPEG:

BitmapFactory.Options options = new BitmapFactory.Options();;
options.inPurgeable = true;

Bitmap original = BitmapFactory.decodeByteArray(input , 0, input.length, options);
Bitmap resized = Bitmap.createScaledBitmap(original, NEW_WIDTH, NEW_HEIGHT, true);

ByteArrayOutputStream blob = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 30, blob);

// recycle the memory
original.recycle();
resized.recycle();