我遇到了OutOfMemoryError,最近使我感到困惑了很长时间。发生OutOfMemoryError时,进程Java堆仍然有很多可用内存,而系统也仍然有很多可用内存。我做了很多工作试图找出答案,但是到目前为止,我仍然不知道出了什么问题。
有4个原因可能导致OutOfMemoryError问题:
1.进程FD号超出限制。
2.进程线程数超出限制。
3.进程Java堆已满。
4.在32位系统中,进程虚拟内存高达3G。
我认为最可能的原因是4,所以我在运行应用程序时检测到进程的虚拟内存,我发现发生OutOfMemoryError时虚拟内存接近3 * 1024 * 1024字节。但是我不知道为什么,有人可以给我一些指导吗?非常感谢!
以下是logcat信息:
04-12 16:15:52.043 9329 9370 W libc : pthread_create failed: couldn't allocate 1069056-bytes mapped space: Out of memory
04-12 16:15:52.065 9329 9370 W zygote : Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"
04-12 16:15:52.106 9329 9370 E AndroidRuntime: FATAL EXCEPTION: EsrEngine
04-12 16:15:52.106 9329 9370 E AndroidRuntime: Process: com.translation.service:offline, PID: 9329
04-12 16:15:52.106 9329 9370 E AndroidRuntime: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at java.lang.Thread.nativeCreate(Native Method)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at java.lang.Thread.start(Thread.java:733)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at com.offline.EsrEngine.notifyWriteTask(EsrEngine.java:278)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at com.offline.EsrEngine.access$600(EsrEngine.java:35)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at com.offline.EsrEngine$3.run(EsrEngine.java:210)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:790)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
04-12 16:15:52.106 9329 9370 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:65)
这是notifyWriteTask()方法的代码。
private void notifyWriteTask() {
isAllowWrite = true;
final BlockingQueue<byte[]> buffer = databuffer;
writeThread = new Thread(new Runnable() {
@Override
public void run() {
EsrLogger.d(TAG, "WriteTask lang=" + currentLang
+ " Run>>>>>>>" + Thread.currentThread().getId()
+ " dataBuffer=" + buffer.size()
+ " isAllow=" + isAllowWrite);
while (isAllowWrite) {
byte[] audio;
try {
audio = buffer.poll(1500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
break;
}
if (!isAllowWrite) {
break;
}
if (audio == null) {
EsrLogger.w(TAG, "WriteTask No Audio over 1500ms =====> Exit");
break;
}
writeAudio(audio, audio.length);
EsrResult result = getEsrResult();
if (result != null && result.isFinish()) {
databuffer.clear();
EsrLogger.d(TAG, "WriteTask has finish result=" + result);
writeAudioFlag = EsrInstBase.ESR_AUDIO_END;
break;
}
}
stopEsr();
buffer.clear();
EsrLogger.d(TAG, "WriteTask End<<<<<<<<" + writeThread.getId());
}
}, currentLang);
writeThread.start();
EsrLogger.d(TAG, "WriteTask Start>>>>>>" + writeThread.getId());
}