Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

android - Memory Leaks & OutOfMemoryError

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("");
    }

    if (source != null){
        sb.append(tools.getText(source));
        sb.append("
");
        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");
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...