Memory Leaks & OutOfMemoryError

2019-08-22 20:38发布

问题:

So I am trying to find out why my app is crashing for

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 128887990 byte allocation with 16777216 free bytes and 76MB until OOM
   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)
   at java.lang.StringBuilder.append(StringBuilder.java:216)    

I have read several post but they all point towards a bitmap image, I am not using any bitmaps, so then I thought maybe I have memory leaks which might cause this, so I installed leakcanary and it is only showing the following leak:

com.google.android.gms.internal.zzcfi.zzfus
references com.google.android.gms.common.api.internal.zzci.zzful
references com.google.android.gms.common.api.internal.zzck.zzfuk
com.tech.activity.dashboard instance

I have searched on Google, Stackoverflow, Github and leak canary and I can not find reference to what this is exactly leaking or how to fix it. I believe this is coming from my google play services location, but could this cause my OOM error I am seeing? Can someone point me in the right direction?

** EDIT ** As a comment pointed out this is supposed to be a string builder issue, I have never changed how my string builder works since I first released the app, here is my Stringbuilder which the source comes from AccessibilityNodeInfo, am I doing something wrong here?

public void processEvent(final AccessibilityNodeInfo source)
{
    final StringBuilder sb = new StringBuilder();
    processSubEvent(source, 0, sb);

    processUIText(source, sb.toString().toLowerCase());
}

private void processSubEvent(final AccessibilityNodeInfo source, final int n, final StringBuilder sb) {
    for (int i = 0; i < n; ++i){
        sb.append("\t");
    }

    if (source != null){
        sb.append(tools.getText(source));
        sb.append("\n");
        final int childCount = source.getChildCount();

        for (int i = 0; i < childCount; i++)
        {
            //Log.e(TAG, "Last UI: " + lastUIText);
            AccessibilityNodeInfo child = source.getChild(i);
            processSubEvent(child, n + 1, sb);

            child.recycle();
            }
        }
}

This is example how the information is being used:

private void processUIText(AccessibilityNodeInfo source, final String text)
{
   if (text.contains("hello") && !text.contains("hello again"){
        tools.showToast("Hello Reached");
   }
}

回答1:

This is example how the information is being used:

Then don't build a string. Build a boolean. Something akin to this should work:

class Whatever {
    private boolean helloSeen=false;
    private boolean helloAgainSeen=false;

    public void processEvent(final AccessibilityNodeInfo source)
    {
        processSubEvent(source);
    }

    private void processSubEvent(final AccessibilityNodeInfo source) {
        if (source != null){
            String text=tools.getText(source);

            if (text.contains("hello")) {
                helloSeen=true;
            }

            if (text.contains("hello again")) {
                helloAgainSeen=true;

            }

            if (!helloSeen || !helloAgainSeen) {
                final int childCount = source.getChildCount();

                for (int i = 0; i < childCount; i++)
                {
                    //Log.e(TAG, "Last UI: " + lastUIText);
                    AccessibilityNodeInfo child = source.getChild(i);
                    processSubEvent(child);

                    child.recycle();
                }
            }
        }
    }
}

After processEvent() returns, helloSeen and helloAgainSeen will reflect whether your messages were encountered anywhere.