致命异常:AsyncTask#2 java.lang.OutOfMemoryError:无法分配JNI Env Android

时间:2016-11-11 09:45:11

标签: java android

11-11 15:11:03.770 10425-10476/com.techworld.example E/Parcel: dup() failed in Parcel::read, i is 0, fds[i] is -1, fd_count is 1, error: Too many open files
11-11 15:11:03.785 10425-10476/com.techworld.example A/OpenGLRenderer: Encountered EGL error 12291 EGL_BAD_ALLOC during rendering
11-11 15:11:03.800 10425-12845/com.techworld.example E/art: ashmem_create_region failed for 'indirect ref table': Too many open files
11-11 15:11:03.800 10425-12845/com.techworld.example E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                       Process: com.techworld.example, PID: 10425
                                                                       java.lang.OutOfMemoryError: Could not allocate JNI Env
                                                                           at java.lang.Thread.nativeCreate(Native Method)
                                                                           at java.lang.Thread.start(Thread.java:1063)
                                                                           at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
                                                                           at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:989)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1131)
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                           at java.lang.Thread.run(Thread.java:818)

第一个我运行异步任务它工作正常,但如果我第二次运行异步任务它显示此错误并强制关闭,不知道是什么导致问题

删除并使用怀疑类的备用内容后我发现这个zip类导致了问题,替换这个类完全正常,不知道这个类中的错误是什么

ZipUtils.java
package com.techworld.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /*
    public static void main(String[] args) throws Exception {
        zipFile("C:/tmp/demo", "C:/tmp/demo.zip", true);
    }
     */

    public static void zipitman(String source,String zipFile, boolean excludeparent) throws Exception {
        zipFile(source, zipFile, excludeparent);
    }

    static public void zipFile(String fileToZip, String zipFile, boolean excludeContainingFolder)
            throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        File srcFile = new File(fileToZip);
        if(excludeContainingFolder && srcFile.isDirectory()) {
            for(String fileName : srcFile.list()) {
                addToZip("", fileToZip + "/" + fileName, zipOut);
            }
        } else {
            addToZip("", fileToZip, zipOut);
        }

        zipOut.flush();
        zipOut.close();

    }

    static private void addToZip(String path, String srcFile, ZipOutputStream zipOut)
            throws IOException {
        File file = new File(srcFile);
        String filePath = "".equals(path) ? file.getName() : path + "/" + file.getName();
        if (file.isDirectory()) {
            for (String fileName : file.list()) {
                addToZip(filePath, srcFile + "/" + fileName, zipOut);
            }
        } else {
            zipOut.putNextEntry(new ZipEntry(filePath));
            FileInputStream in = new FileInputStream(srcFile);

            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) != -1) {
                zipOut.write(buffer, 0, len);
            }
        }
    }
}

0 个答案:

没有答案