我在android上遇到以下错误:
Out of memory: Heap Size=41159KB, Allocated=33002KB, Limit=49152KB
当我尝试调用构造函数的BitmapFactory.decodeResource
方法时:
public class Background {
private Bitmap bitmap;
public Background(Resources resources) {
bitmap = BitmapFactory.decodeResource(resources, R.drawable.ground);
}
public void render(Canvas canvas, int WIDTH, int HEIGHT) {
for(int x = 0; x < WIDTH; x += bitmap.getWidth())
for(int y = 0; y < HEIGHT; y += bitmap.getHeight())
canvas.drawBitmap(bitmap, x, y, null);
}
}
任何解决方案?
在我尝试这样做之前遇到同样的错误之前:
socket.on("render", new Emitter.Listener() {
@Override
public void call(Object... args) {
update(args);
Canvas canvas = getHolder().lockCanvas();
draw(canvas);
getHolder().unlockCanvasAndPost(canvas);
}
});
使用socket.io我在服务器上创建了渲染循环,向所有客户端套接字发出“render”事件。现在它甚至没有达到应该自我渲染的程度。
所以上面提到了两个问题,任何想法如何解决这个问题? 提前谢谢你......
修改
android:largeHeap =“true”只是在10秒后推开这个问题
编辑2
System.gc()使它持续更长时间,比如15秒,直到它失败:
14 21:01:21.340 12844-18714/com.main.fishyio E/dalvikvm-heap: Out of memory on a 516182-byte allocation.
06-14 21:01:21.340 12844-18714/com.main.fishyio E/dalvikvm: Out of memory: Heap Size=130567KB, Allocated=116497KB, Limit=49152KB
06-14 21:01:21.340 12844-18714/com.main.fishyio E/dalvikvm: Extra info: Footprint=130567KB, Allowed Footprint=130567KB, Trimmed=0KB
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: crash in the same process: EventThread
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: java.lang.OutOfMemoryError: (Heap Size=130567KB, Allocated=116497KB)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.String.<init>(String.java:432)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.StringBuilder.toString(StringBuilder.java:663)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.util.Formatter.toString(Formatter.java:935)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.String.format(String.java:1998)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.String.format(String.java:1972)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.socketio.parser.Parser$Decoder.decodeString(Parser.java:231)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.socketio.parser.Parser$Decoder.add(Parser.java:148)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.socketio.client.Manager.ondata(Manager.java:339)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.socketio.client.Manager.access$1100(Manager.java:19)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.socketio.client.Manager$2.call(Manager.java:312)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.Socket.onPacket(Socket.java:482)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.Socket.access$800(Socket.java:29)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.Socket$5.call(Socket.java:285)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.Transport.onPacket(Transport.java:121)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.Transport.onData(Transport.java:113)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.transports.WebSocket.access$100(WebSocket.java:19)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.engineio.client.transports.WebSocket$1$3.run(WebSocket.java:74)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at com.github.nkzawa.thread.EventThread$2.run(EventThread.java:75)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-14 21:01:21.470 12844-18714/com.main.fishyio E/AndroidRuntime_3_crash: at java.lang.Thread.run(Thread.java:864)
虽然我还没有渲染精灵......
编辑3
在单独的线程中运行渲染循环没有帮助。所以我尽量不画背景和前景但它也失败了。正如我之前所说,当我尝试在服务器上运行渲染循环时,问题似乎出现在Socket.io库中。
编辑4
如果我将服务器上的FPS更改为等于1,则不会失败!!!