字节[]和java.lang.OutOfMemoryError由位读/写文件 [英] Byte[] and java.lang.OutOfMemoryError read/write file by bits

查看:214
本文介绍了字节[]和java.lang.OutOfMemoryError由位读/写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作抹在android的一些自由空间。这里是我的code:

 私人无效creatingFileDelete(INT大小,INT passMode)
    {
        文件lastFile =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()/ tmpToDelete / last.txt。);
        如果(lastFile.exists()== FALSE)
        {
        尝试
        {
            lastFile.createNewFile();
        } // try块结束
        赶上(IOException异常E)
        {
            e.printStackTrace();
        } // catch块结束
    } //如果条件结束    尝试
    {
        RandomAccessFile的rwFile =新RandomAccessFile的(lastFile,RW);
        rwFile.setLength(1024 * 1024 *大小);
        FileChannel rwChannel = rwFile.getChannel();
        INT的numBytes =(int)的rwChannel.size();
        MappedByteBuffer缓冲= rwChannel.map(FileChannel.MapMode.READ_WRITE,0的numBytes);
        buffer.clear();
        字节[] = randomBytes新的字节[的numBytes]        新的随机()的nextBytes(randomBytes);
        Arrays.fill(randomBytes,0,randomBytes.length,(字节)passMode);
        buffer.put(randomBytes);        buffer.force();
        rwFile.close();
    } // try块结束
    赶上(例外五)
    {
        Log.e(清洁自由空间---,消息:+ e.getMessage());
    } // catch块结束
} // creatingFileDelete结束

在擦,我得到了下面的崩溃。

 了java.lang.RuntimeException:执行doInBackground发生错误()
          在android.os.AsyncTask $ 3.done(AsyncTask.java:299)
          在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
          在java.util.concurrent.FutureTask.setException(FutureTask.java:219)
          在java.util.concurrent.FutureTask.run(FutureTask.java:239)
          在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
          在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
          在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573)
          在java.lang.Thread.run(Thread.java:856)
    java.lang.OutOfMemoryError:产生的原因
          在xxx.creatingFileDelete(CleanFreespace.java:2634)
          在xxx.CleanFreespace.access $ 71(CleanFreespace.java:2610)
          在xxx.CleanFreespace $ 15.doInBackground(CleanFreespace.java:2100)
          在xxx.CleanFreespace $ 15.doInBackground(CleanFreespace.java:1)
          在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
          在java.util.concurrent.FutureTask.run(FutureTask.java:234)
          ... 4个

朋友我怎么能解决这个问题。


解决方案

 的OutOfMemoryError


  

当内存发出请求不能利用现有的平台资源来满足时抛出。这样的请求可以由两个运行的应用程序或由VM(虚拟机)

的内部函数进行

当你得到像内存不足错误就意味着你正在运行的内存应用为您的应用程序占用更多的空间,然后由系统分配给它。

把这个属性的android:largeHeap =真正的中的<应用/> atgin的的manifest.xml 文件

例如:

 <应用
    机器人:名字=support.classes.StartUp
    机器人:allowBackup =真
    机器人:图标=@的mipmap / ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:largeHeap =真//你的应用程序增加堆空间
    机器人:主题=@风格/ AppThemeTest>

如果您简单想释放一些空间,这将是更好地让Android系统处理它它知道最好的。

 的System.gc()

调用垃圾收集你做任何消耗内存的任务之前,因为这将释放一些空间给你。如果你尝试你的任务之后调用这个话也是没有用处的。

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.

解决方案

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

这篇关于字节[]和java.lang.OutOfMemoryError由位读/写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆