太多的日志写入会降低Android应用程序的性能吗?

发布于 2024-12-11 06:49:04 字数 57 浏览 0 评论 0原文

我想知道日志记录是否会降低应用程序性能? 另外,请给我一些提高 Android 应用程序性能的技巧。

I would to know whether logging can decrease application performance?
Also, please give me some tips to increase android application performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-12-18 06:49:04

是的。过多的日志记录会影响任何应用程序的性能,而不仅仅是 Android。

开发人员指南建议您在发布之前停用并禁用日志记录:

关闭日志记录和调试确保您停用日志记录并
在构建应用程序之前禁用调试选项
发布。您可以通过删除对 Log 方法的调用来停用日志记录
在你的源文件中。您可以通过删除来禁用调试
android:debuggable 属性来自您的标签
清单文件,或者通过将 android:debuggable 属性设置为 false
在你的清单文件中。另外,删除所有日志文件或静态测试文件
在您的项目中创建的。

此外,您应该删除添加到您的
代码,例如 startMethodTracing() 和 stopMethodTracing() 方法
来电。

因此,您应该抑制应用程序的“发布”或“生产”版本中的日志。

通过设置 debuggable 在 Android Manifest 中关闭它:

<application android:icon="@drawable/icon" 
        android:label="@string/app_name"
        android:debuggable="false">

另一种方法

创建自己的记录器类并在执行日志之前检查调试模式。它允许调试模式和部署的应用程序之间的单点修改,并允许执行额外的操作,例如写入日志文件。

import android.util.Log;
public class MyLog {
    private static final boolean isDebug = false;;
    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }
    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.e(tag, msg);
        }
    }
}


For further info read http://rxwen.blogspot.com/2009/11/logging-in-android.html

和 SO QA:

Log.d 和对性能的影响

Android 日志记录 - 如何清除以获得更好的性能

Yes. Excessive logging affects the performance of any application not just Android.

The developer guide suggests you to deactivate and disabled logging before release:

Turn off logging and debugging Make sure you deactivate logging and
disable the debugging option before you build your application for
release. You can deactivate logging by removing calls to Log methods
in your source files. You can disable debugging by removing the
android:debuggable attribute from the tag in your
manifest file, or by setting the android:debuggable attribute to false
in your manifest file. Also, remove any log files or static test files
that were created in your project.

Also, you should remove all Debug tracing calls that you added to your
code, such as startMethodTracing() and stopMethodTracing() method
calls.

So, you should suppress the logs in "release" or "production" build of your App.

Turn off it in Android Manifest by setting debuggable:

<application android:icon="@drawable/icon" 
        android:label="@string/app_name"
        android:debuggable="false">

Another way

Create your own logger class and check for debugging mode before executing log. It allows single point modification between debugging mode and deployed application and allows extra things to do such as writing to log file.

import android.util.Log;
public class MyLog {
    private static final boolean isDebug = false;;
    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }
    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.e(tag, msg);
        }
    }
}


For further info read http://rxwen.blogspot.com/2009/11/logging-in-android.html

and The SO QAs :

Log.d and impact on performance

Android Logging - How to clear for better performance

耶耶耶 2024-12-18 06:49:04

在写入日志之前使用“if”语句。
您可以在应用程序发布时禁用日志。

例子 :

public class LogTest extends Activity {

private static final String TAG = "YOUR_LOG_TAG_NAME";
private static final boolean mDebug = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //--[Start Log example]-----------------------------------------
    if(mDebug) Log.d(TAG, "Log Write Test");
    //--[End Log example]-----------------------------------------
}

}

use 'if' statement before log writing.
you can disable log when application release.

example :

public class LogTest extends Activity {

private static final String TAG = "YOUR_LOG_TAG_NAME";
private static final boolean mDebug = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //--[Start Log example]-----------------------------------------
    if(mDebug) Log.d(TAG, "Log Write Test");
    //--[End Log example]-----------------------------------------
}

}

不一样的天空 2024-12-18 06:49:04

任何过多的文本输出都会减慢您的应用程序的速度,即使对于桌面应用程序也是如此。过多的日志记录确实会减慢速度,但您基本上需要发送大量数据,顺便说一句,使用 for 或 while 循环并不难做到这一点。有时记录时在后台发生的事情包括字符串操作、文本渲染、缓存管理、数据过滤、日志长度限制等等。

有多种方法可以从最终应用程序中删除 logcat,尤其是涉及 ProGuard 的方法。 ProGuard 不适合我,但是关于如何删除它们有很多绝妙的想法,包括像 sed 这样的脚本程序。

Any text output in excess will slow down your application, even with a desktop application. Excessive logging does slow things down, but you would basically need an insane amount of data being sent through, which by the way, isn't that hard to do with a for or while loop. Things that happen in the background while logging sometime include string manipulation, text rendering, cache management, data filtering, log length limiting, and the list goes on.

There's several ways to remove logcat from your final app, especially those involving ProGuard. ProGuard didn't work for me, but there's plenty of brilliant ideas out there on how to remove them including scripting programs like sed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文