Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory leaking thread
    primarykey
    data
    text
    <p>I have a TabHost with TextViews associated to each tab, in an Activity context. Currently there's also an AsyncTask which manages the TCP socket and when it receives some kind of information, it's sent to the main Activity handler, who calls a method to append the text to the current TextView.</p> <p>Well, after a while (approx 5 minutes) of running, I notice how it starts hanging and finally I get an ANR. So I installed Memory Analyzer for Eclipse and in conjunction with DDMS, I was able to find out what seems to be the bottleneck, but not how to deal with it.</p> <p>Using the previously mentioned tools, I was able to see hundreds of lines like the ones on this pic:</p> <p><img src="https://i.stack.imgur.com/IVntt.png" alt="enter image description here"></p> <p><strong>BTW</strong>: There's a strange detail in that pic, the Thread Id is always 1; shouldn't it be different for each thread as the append is called on different threads?</p> <p>Those hundreds of lines reference to the two 'appends' you may see in the code above. I'm new to the thread world, so if you see something weird, I'd appreciate any advice. This is the code where the bottleneck is:</p> <pre><code>// tab = is the name of the tab where to append the new text // line = the line to append; it may contain spans, colors, bold text, etc... that's why it's a SpannableStringBuilder // ColorLine is a method that just returns a SpannableStringBuilder-formatted string synchronized private static void Write2Tab(final String tab, final SpannableStringBuilder line) { final TabHost lth = (TabHost) activity.findViewById(android.R.id.tabhost); final TextView tabContent = (TextView) lth.getCurrentView(); new Thread(new Runnable() { public void run() { String curtab = getCurrentTabName(); // If the current tab is the one where we need to write, we do if (tab.equalsIgnoreCase(curtab)) { activity.runOnUiThread(new Runnable() { public void run() { tabContent.append(linea); tabContent.append((ColorLine("\n", Color.WHITE, false, 0, 1))); } }); } } }).start(); [...] } </code></pre> <p>I've had 2 more memory leaks in my code that I was able to fix, but this one's starting to be a bit hard... Any ideas of why is this happening and how to fix it?</p> <p><strong>--------------- EDIT ---------------</strong></p> <p>After several days of frustrating research about leaks in my app, I found out there was more than one. I managed to solve them, the one I originally posted had to do with John Vint's post, there was an issue with the buffering control which made a substancial accumulation of SpannableStringBuilder objects in the TextView, so I'm accepting his post. Now I have a different issue with another curious leak but it has nothing to do with the original post so I'm going to open a new question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload