当我创建对象以写入输出流(大小低于 150KB)时,它可能会立即被销毁,因为:
System.out.println("Sending object..");
Packet p = new Packet(mJpegData); // here exists
System.out.println(p); // reference exists
oos.writeObject(p); // null exception error
完整错误:
04-26 21:37:01.414: W/System.err(12888): java.lang.NullPointerException
04-26 21:37:01.414: W/System.err(12888): at pl.aadamczyk.webcamera.Preview$PreCallback.onPreviewFrame(Preview.java:148)
04-26 21:37:01.417: W/System.err(12888): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:583)
04-26 21:37:01.417: W/System.err(12888): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 21:37:01.417: W/System.err(12888): at android.os.Looper.loop(Looper.java:130)
04-26 21:37:01.417: W/System.err(12888): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-26 21:37:01.417: W/System.err(12888): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 21:37:01.417: W/System.err(12888): at java.lang.reflect.Method.invoke(Method.java:507)
04-26 21:37:01.417: W/System.err(12888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-26 21:37:01.417: W/System.err(12888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-26 21:37:01.417: W/System.err(12888): at dalvik.system.NativeStart.main(Native Method)
04-26 21:37:01.472: D/dalvikvm(12888): GC_FOR_MALLOC freed 1152K, 57% free 2820K/6535K, external 1596K/2108K, paused 17ms
04-26 21:37:01.472: I/System.out(12888): My Object has been GC'd. Packet@4063d908
04-26 21:37:01.523: D/dalvikvm(12888): GC_CONCURRENT freed 28K, 50% free 3311K/6535K, external 1596K/2108K, paused 3ms+10ms
“My Object has been GC'd.”由 Packet 的终结器打印。
整个代码: http: //pastebin.com/zc5TcSgY
为什么 GS 正在移除我的对象?我有大约 120 MB 的可用内存。
如何解决?