Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 内存不足_Android_Heap Memory - Fatal编程技术网

Android 内存不足

Android 内存不足,android,heap-memory,Android,Heap Memory,我有一个简单的java程序,它依赖于inputstream。我必须读入一个160 MB的ecg文件,它在普通jvm上工作得非常好,但当我在android上运行这段代码时,它根本无法分配160 MB的内存并关闭我的应用程序 这是一个被截取的代码: // reads the ecg file into a stream and stores it in the InputArray public byte[] readStream() throws IOException{

我有一个简单的java程序,它依赖于inputstream。我必须读入一个160 MB的ecg文件,它在普通jvm上工作得非常好,但当我在android上运行这段代码时,它根本无法分配160 MB的内存并关闭我的应用程序

这是一个被截取的代码:

//      reads the ecg file into a stream and stores it in the InputArray
    public byte[] readStream() throws IOException{
        FileInputStream inputFile = new FileInputStream(getDataPath());
        setBytesToSkip(0);
        inputFile.skip(getBytesToSkip());
        setLengthOfInputFile(inputFile.available());
        byte[] InputArray = new byte[getLengthOfInputFile()];
        setInputArray(InputArray);
        inputFile.read(getInputArray());
        inputFile.close();
        return inputArray;

    }
//              writes the bytes of the inputArray into a buffer bb
            public ByteBuffer bufferStream(byte[] array){
        inputArray = array;
        setBufferOffset(0);
        setBufferByteReadLength(getLengthOfInputFile());

        ByteBuffer bb = ByteBuffer.allocateDirect(getBufferByteReadLength());
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.put(getInputArray(), getBufferOffset(), getBufferByteReadLength());
        return bb;
    }
我还尝试使用DirectBuffer绕过正常堆,但仍然出现内存溢出错误,如:

dalvikvm-heap  PID:1715  Out of memory on a 167230992-byte allocation
有没有办法以更有效的方式处理inputstream的数据?或者我可以将一些存储
例如,SD卡作为“堆”


向Lorezo致意

虚拟机堆将无法分配160 MB大小的对象,因为许多设备都有32或64 MB的堆。尝试在循环中读取文件(例如4MB块)并跳过在下一次迭代中已读取的字节数。

通常,这是错误的方法。这些设备没有那么多内存可供应用程序使用,如果有,您需要记住,您需要与手机上运行的所有其他应用程序共享这些内存。然而,这里有几件事我马上就注意到了

首先,您尝试两次分配160MB

一旦来到这里:

byte[] InputArray = new byte[getLengthOfInputFile()];
在这里:

ByteBuffer bb = ByteBuffer.allocateDirect(getBufferByteReadLength());
您可以尝试重新排列此代码,这样就不会分配两次内存


您还需要在清单中设置android:largeHeap=“true”。

@MT8使用
AsyncTask
不会给应用程序更多内存。
AsyncTask
在这里有什么帮助?为什么需要在内存中保存整个文件?读完后你会怎么处理它?记住,这个设备是一部手机!