簡體   English   中英

我的應用中釋放的ANDROID GC_FOR_ALLOC錯誤

[英]ANDROID GC_FOR_ALLOC freed in my app going wrong

那是我的android代碼,需要30分鍾才能將3MB .log文件復制到.zip並提供許多GC_FOR_ALLOC 我也嘗試將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();

在您的代碼中,您好像打開和讀取了兩次文件(您的BufferedReader和您的iStream對象)。 此外,在將任何內容寫入內存之前,您都將整個文件兩次加載到內存中。 那仍然只有6MB,但您可能正在達到內存堆棧限制-除非您在清單中使用android:largeHeap="true"

在執行此操作之前,請嘗試僅閱讀/編寫每個部分:

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

這是參考: 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