3

I working on wiping some free space in android. Here is my code:

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 

During wiping, i got the following crash.

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 how i can resolve this issue.

1 Answer 1

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

when you get error like OutOfMemory it means you are running out of memory for the application as your app consumes more space then allocated to it by the system.

put this attribute android:largeHeap="true" in the the <application/> atgin the manifest.xml file

for example

 <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">

if you simple want to free some space it would be better to let Android system handle it which knows the best

System.gc()

call garbage collector before you do any memory consuming task as that will free some space for you. if you try to call this after your task then it serves no purpose

Sign up to request clarification or add additional context in comments.

6 Comments

Pankaj, Again i'm getting following crash : Caused by: java.lang.OutOfMemoryError: Failed to allocate a 447741964 byte allocation with 5077506 free bytes and 120MB until OOM
@Gowtham, you don't have to have write all the low level code dealing with bytes, common-io-2.4 can work work for you, and make things quite easy
why not, can you tell me what exactly you are trying to do, i will what i can offer
How to wipe the free space in android, which shouldn't be recovered by any software.
when you say wipe are you trying to free the space which was already free. Or are you trying to write data which would not be accessed by other apps and user ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.