简体   繁体   中英

how to prevent GC_CONCURRENT statements in Logcat

I have a requirement that I need to check the no.of lines in the File 'A' and if File 'A' exceeds my limit then I need to copy its contents into other file 'B' and then clear the contents of file 'A'.

The above task I have to execute all the times So , I constructed "Service" to do this task. (I want to run this in back ground).

From the Service I am launching a thread to execute the above task.( I have other task in Service which should run in parallel along with the task).

I am using AlarmManager to keep my "Service" alive.

Bottom line is that above task will run all the time. So far I succeeded in what I want to achieve.

But I have observed in the LogCat output that It is generating huge statements related to GC.

Like:

D/dalvikvm( 2579): GC_CONCURRENT freed 483K, 62% free 2608K/6727K, external 1628K/2108K, paused 2ms+2ms
D/dalvikvm( 2579): GC_CONCURRENT freed 469K, 62% free 2608K/6727K, external 1628K/2108K, paused 34ms+2ms
D/dalvikvm( 2579): GC_CONCURRENT freed 466K, 62% free 2608K/6727K, external 1628K/2108K, paused 1ms+2ms
D/dalvikvm( 2579): GC_CONCURRENT freed 472K, 62% free 2609K/6727K, external 1628K/2108K, paused 7ms+2ms
D/dalvikvm( 2579): GC_CONCURRENT freed 473K, 62% free 2607K/6727K, external 1628K/2108K, paused 2ms+1ms
D/dalvikvm( 2579): GC_CONCURRENT freed 473K, 62% free 2607K/6727K, external 1628K/2108K, paused 1ms+2ms
D/dalvikvm(  263): GC_EXPLICIT freed 351K, 58% free 3118K/7303K, external 10278K/11844K, paused 42ms 

I suspect my application leaking some memory or it is consuming heavy memory. with respect to performance I want to avoid this . Below is the code snippet that I am running all the time in thread.

public void run()
{
    while (true) {
        if (WifiLogCollector.stoopThread)
            return;
        LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(textfile));
        lineNumberReader.skip(Long.MAX_VALUE);

        if ((lineNumberReader.getLineNumber())  > 20000) {

            FileChannel source = null;
            FileChannel destination = null;

            try {
                source = new FileInputStream("/mnt/sdcard/textfile.txt").getChannel();
                destination = new FileOutputStream("/mnt/sdcard/textbackupfile.txt")
                                .getChannel();
                destination.transferFrom(source, 0, source.size());
            } finally {
                if (source != null) {
                    source.close();
                    source = null;
                }
                if (destination != null) {
                    destination.close();
                    destination = null;
                }
            }

            PrintWriter writer = new PrintWriter(/mnt/sdcard/textfile.txt);
            writer.print("");
            writer.close();
            writer = null;

        }

        lineNumberReader.close();
        lineNumberReader = null;
    }
}

the above is the run() method of my thread.

Please help me what should be the changes I need do in my design to avoid GC logs and make my app memory efficient.

You are creating too many "new" objects in your loop which is causing the GC to run wild cleaning them up after you've nulled them out. You want to make them static class variables, not new local variables so you can keep using the same objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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