![](/img/trans.png)
[英]Byte[] and java.lang.OutOfMemoryError reading file by bits
[英]Byte[] and java.lang.OutOfMemoryError read/write file by bits
我正在努力擦除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
朋友我怎么能解決這個問題。
OutOfMemoryError
在使用可用平台資源無法滿足內存請求時拋出。 這樣的請求可以由正在運行的應用程序或VM的內部功能(虛擬機)進行
當你收到像OutOfMemory
這樣的OutOfMemory
這意味着你的應用程序內存不足,因為你的應用程序會消耗更多的空間,然后由系統分配給它。
在manifest.xml
文件的<application/>
atgin中放置此屬性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()
在執行任何內存消耗任務之前調用垃圾收集器,因為這將為您釋放一些空間。 如果你試圖在你的任務之后調用它,那么它沒有任何意義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.