如何美元的logcat中p $ pvent GC_CONCURRENT声明声明、美元、pvent、logcat

2023-09-06 10:44:16 作者:蛊醉

我有我需要检查的文件A,如果文件的NO.OF行的要求A超出我的极限,然后我需要将其内容复制到另一个文件'B',然后清除内容文件的A。

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

我使用AlarmManager保持我的服务还活着。

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.

但是我在LogCat中输出,这是产生与GC巨大的语句观察。

像:

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 

我怀疑我的应用程序泄露一些内存或消耗沉重的记忆。 在性能方面,我想避免这种情况。 下面是我运行所有的时间在线程中的code段。

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

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

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

请帮我应该是什么,我需要做我的设计,避免GC日志,让我的应用程序的内存效率的变化。

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撒野清除它们,你已经调零出来后。你想使它们的静态类变量,而不是新的局部变量,这样你就可以继续使用相同的对象。

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.