Byte[]和java.lang.OutOfMemoryError通过位读/写文件

3

我正在清理安卓系统中的一些空闲空间。以下是我的代码:

private void creatingFileDelete(int size, int passMode) 
    {
        File lastFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"/.tmpToDelete/last.txt");
        if (lastFile.exists() == false) 
        {
        try 
        {
            lastFile.createNewFile();
        }//End of try block
        catch (IOException e) 
        {
            e.printStackTrace();
        }//End of catch block
    }//End of if condition

    try 
    {
        RandomAccessFile rwFile = new RandomAccessFile(lastFile, "rw");
        rwFile.setLength(1024*1024*size);
        FileChannel rwChannel = rwFile.getChannel();
        int numBytes = (int) rwChannel.size();
        MappedByteBuffer buffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, numBytes);
        buffer.clear();
        byte[] randomBytes = new byte[numBytes];

        new Random().nextBytes(randomBytes);
        Arrays.fill(randomBytes, 0, randomBytes.length, (byte) passMode);
        buffer.put(randomBytes);

        buffer.force();
        rwFile.close();
    }//End of try block
    catch (Exception e) 
    {
        Log.e("Clean free space---","message :" +e.getMessage());
    }//End of catch block
}//End of creatingFileDelete 

在擦除期间,我遇到了以下崩溃。
java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:299)
          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
          at java.util.concurrent.FutureTask.run(FutureTask.java:239)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
          at java.lang.Thread.run(Thread.java:856)
    Caused by: java.lang.OutOfMemoryError
          at xxx.creatingFileDelete(CleanFreespace.java:2634)
          at xxx.CleanFreespace.access$71(CleanFreespace.java:2610)
          at xxx.CleanFreespace$15.doInBackground(CleanFreespace.java:2100)
          at xxx.CleanFreespace$15.doInBackground(CleanFreespace.java:1)
          at android.os.AsyncTask$2.call(AsyncTask.java:287)
          at java.util.concurrent.FutureTask.run(FutureTask.java:234)
          ... 4 more

朋友,我该如何解决这个问题。

1个回答

3
OutOfMemoryError 

当系统资源不足以满足运行应用程序或虚拟机(VM)内部函数所需的内存时,会发生“OutOfMemory”错误。
当您收到“OutOfMemory”类似的错误时,这意味着您的应用程序消耗的空间超过了系统分配给它的空间,因此导致内存不足。
在“manifest.xml”文件中,在标签中添加android:largeHeap="true"属性。
例如:
 <application
    android:name="support.classes.StartUp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true" // you are increasing the heap space for the app 
    android:theme="@style/AppThemeTest">

如果你只是想释放一些空间,最好让Android系统处理它,因为它知道最好的方法。

System.gc()

在执行任何消耗内存的任务之前,请调用垃圾回收器,这样可以为您释放一些空间。如果在任务完成后再调用它,则没有任何作用。

Pankaj,我又遇到了以下崩溃:Caused by: java.lang.OutOfMemoryError: Failed to allocate a 447741964 byte allocation with 5077506 free bytes and 120MB until OOM。 - Gowtham
@Gowtham,你不需要编写所有与字节打交道的低级代码,common-io-2.4可以为你工作,并使事情变得相当容易。 - Pankaj Nimgade
为什么不呢?你能告诉我你想做什么吗?我会尽力提供帮助。 - Pankaj Nimgade
如何在Android中擦除空闲空间,以便任何软件都无法恢复。 - Gowtham
当你说擦除时,你是想要释放已经空闲的空间吗?还是你想要写入数据,以便其他应用程序和用户无法访问? - Pankaj Nimgade
我正在尝试写入不可访问的数据。 - Gowtham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接