gpt4 book ai didi

java - Byte[] 和 java.lang.OutOfMemoryError 按位读/写文件

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:52 25 4
gpt4 key购买 nike

我正致力于清除 android 中的一些可用空间。这是我的代码:

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

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

最佳答案

OutOfMemoryError 

Thrown when a request for memory is made that can not be satisfied using the available platform resources. Such a request may be made by both the running application or by an internal function of the VM(Virtual Machine)

当你得到类似 OutOfMemory 的错误时这意味着您的应用程序内存不足,因为您的应用程序消耗了更多空间然后由系统分配给它。

把这个属性android:largeHeap="true"<application/>开始manifest.xml文件

例如

 <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()

在执行任何内存消耗任务之前调用垃圾收集器,因为这将为您释放一些空间。如果您尝试在完成任务后调用它,那么它没有任何用处

关于java - Byte[] 和 java.lang.OutOfMemoryError 按位读/写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com