简体   繁体   English

我的应用中释放的ANDROID GC_FOR_ALLOC错误

[英]ANDROID GC_FOR_ALLOC freed in my app going wrong

That's my android code which takes 30 min to copy a 3MB .log file into a .zip and gives lots of GC_FOR_ALLOC . 那是我的android代码,需要30分钟才能将3MB .log文件复制到.zip并提供许多GC_FOR_ALLOC I also tried to change buffersize from 1k to 8k 我也尝试将buffersize从1k更改为8k

File tempFolder=new File(Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP); 
String filePath= Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP+ "/";
String fileName ="";
String zipFileName="";

String date=DATE_FORMAT_TEMP.format(new Date());
fileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.log; 
zipFileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.zip;

br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String result="";
String line = "";
while((line = br.readLine())!=null)
    result += line;

result = getHeader() + result;
fos = new FileOutputStream(file);
fos.write(result.getBytes());

File zipFile=new File(filePath+zipFileName);

iStream = new FileInputStream(file);
oStream = new FileOutputStream(zipFile);

zos = new ZipOutputStream(oStream);
ze = new ZipEntry(fileName);
zos.putNextEntry(ze);

byte[] buffer = new byte[1024];
while((length = iStream.read(buffer)) != -1)
    zos.write(buffer, 0, length);
zos.flush();
oStream.flush();

In your code it looks like you are opening and reading the file twice (your BufferedReader and your iStream object). 在您的代码中,您好像打开和读取了两次文件(您的BufferedReader和您的iStream对象)。 Also, you are loading the entire file into memory twice before writing anything to memory. 此外,在将任何内容写入内存之前,您都将整个文件两次加载到内存中。 That's still only 6MB but you probably are hitting your memory stack limit - unless you use android:largeHeap="true" in your manifest. 那仍然只有6MB,但您可能正在达到内存堆栈限制-除非您在清单中使用android:largeHeap="true"

Before you do that though, try just reading/writing each part: 在执行此操作之前,请尝试仅阅读/编写每个部分:

    public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.length; i++) {
                Log.v("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Here is the reference: http://javatechig.com/android/how-to-programmatically-zip-and-unzip-file-in-android 这是参考: http : //javatechig.com/android/how-to-programmatically-zip-and-unzip-file-in-android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM