java 如何防止 Logcat 中的 GC_CONCURRENT 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7110351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:40:43  来源:igfitidea点击:

how to prevent GC_CONCURRENT statements in Logcat

javaandroid

提问by brig

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'.

我有一个要求,我需要检查文件“A”中的行数,如果文件“A”超出我的限制,那么我需要将其内容复制到其他文件“B”中,然后清除文件“的内容”一个'。

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

我正在从 Service 启动一个线程来执行上述任务。(我在 Service 中有其他任务应该与任务并行运行)。

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

我正在使用 AlarmManager 来保持我的“服务”活着。

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.

但是我在 LogCat 输出中观察到它正在生成与 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.

以上是我的线程的 run() 方法。

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

请帮助我在我的设计中需要做哪些更改以避免 GC 日志并使我的应用程序内存高效。

采纳答案by Chris

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.

您在循环中创建了太多“新”对象,这导致 GC 在您将它们清零后疯狂地清理它们。你想让它们成为静态类变量,而不是新的局部变量,这样你就可以继续使用相同的对象。