【问题标题】:Android Out of Memory EventsAndroid 内存不足事件
【发布时间】:2014-03-10 17:25:28
【问题描述】:

是否有任何方法可以获取有关发生的内存不足事件的统计信息,甚至 如果只有多少?

我主要关心事件的数量和发生的时间。这是 在我正在构建的应用中需要。

是否有一些 API 支持或者我需要解析一些文件?

【问题讨论】:

    标签: java android out-of-memory


    【解决方案1】:

    如果您可以在开发环境中重现 OOM 错误,那么您可以使用 Logcat 查看 GC 消息,并可能在您的应用程序中记录事件。 DDMS Eclipse 视图中还提供了许多工具,可帮助您跟踪分配情况并准确查看哪些对象占用了内存。

    android dev site 上有大量信息。

    【讨论】:

    • 感谢您的回复。我需要将所有应用程序的 OOM 错误计数读入我的应用程序,这在生产中是必需的。这就是我要问的。
    • 您想要从最终用户发送的分析信息,例如用户做某事的次数?
    • 我想知道是否有地方或方法可以访问/计算有关在 Android 中发生 outOfMemoryError 的次数的信息。我在我的应用中需要这些信息。
    【解决方案2】:

    您可以使用Crashlytics 之类的东西,它免费且易于设置。它会自动向您发送崩溃报告,而不向用户提示任何内容,并提供有关手机的完整堆栈跟踪和统计信息(即使没有互联网连接)。

    或者类似的东西可以捕捉到一切:

    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.app.Activity;
    import android.content.Context;
    
    public class SRSDexception implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler defaultUEH;
    private Activity app = null;
    
    public SRSDexception(Activity app) {
    
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    this.app = app;
    }
    
    public void uncaughtException(Thread t, Throwable e) 
    {   
    
    StackTraceElement[] arr = e.getStackTrace();
    String Raghav =t.toString();
    String report = e.toString()+"\n\n";
    report += "--------- Stack trace ---------\n\n"+Raghav;
    
    for (int i=0; i<arr.length; i++)
    {
    report += "    "+arr[i].toString()+"\n";
    }
    report += "-------------------------------\n\n";
    
    // If the exception was thrown in a background thread inside
    // AsyncTask, then the actual exception can be found with getCause
    report += "--------- Cause ---------\n\n";
    Throwable cause = e.getCause();
    if(cause != null) {
    report += cause.toString() + "\n\n";
    arr = cause.getStackTrace();
    for (int i=0; i<arr.length; i++)
    {
    report += "    "+arr[i].toString()+"\n";
    }
    }
    report += "-------------------------------\n\n";
    
    try {
    FileOutputStream trace = app.openFileOutput(
    "stack.trace", Context.MODE_PRIVATE);
    trace.write(report.getBytes());
    trace.close();
    } catch(IOException ioe) {
    // ...
    }
    
    defaultUEH.uncaughtException(t, e);
    }
    
    }
    

    找到here

    【讨论】:

    • 我主要关心事件的数量和发生时间。有什么方法可以让我自己做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2013-05-08
    相关资源
    最近更新 更多