If I run the following code on my HTC magic phone I get a java.lang.OutOfMemoryError error. Is this simple short piece of code too much to ask for, for my phone to handle?
I think the code is simple and pretty efficient, but my phone simply won’t eat it.
If I cut the code into half the amount (just after the first for loop) the phone eats it.
I checked my phones memory and it says 79 MB free with the help from the “Open Advanced Task Killer” app.
I do not have a lot of apps open simultaneously and the code I’m trying to execute is not a lot to ask for (in my opinion).
Do I really have to buy a new phone or is there an alternative to handle this problem?
public class SomeActivity extends Activity
{
public TextView tv;
public ImageView imgv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textview);
imgv = (ImageView)findViewById(R.id.imageView1);
tv.setText("This is the new text");
try
{
String selectedGenre = "http://www.somewebpage.com/";
Document doc = Jsoup.connect(selectedGenre).timeout(0).get();
Elements liElements = doc.select("ul#navbar li");
String pagesAmount = null;
int liElementsCounter = 0;
for (Element element : liElements)
{
if (liElementsCounter < 1 && element.html().contains("Page ("))
{
String liElementText = element.html();
pagesAmount = liElementText.substring((liElementText.indexOf("/") + 2), (liElementText.lastIndexOf(")") - 1));
liElementsCounter++;
}
}
doc = Jsoup.connect(selectedGenre.replace("p1", "p" + new Random().nextInt(1 + Integer.parseInt(pagesAmount)))).timeout(0).get();
Elements aNameElements = doc.select("a[name]");
ArrayList<String> elementsArray = new ArrayList<String>();
int elementsSize = 0;
for (Element element : aNameElements)
{
if (elementsSize % 3 == 0)
{
elementsArray.add(element.attr("href").replaceAll("\\s",""));
}
elementsSize++;
}
}
catch (Exception ex)
{
tv.setText(ex.getMessage());
System.out.println("Exception! " + ex.getMessage());
}
}
}
This is just 1/15 of the total amount of code I really wanna execute
Logcat:
06-22 14:23:18.323: D/InetAddress(2614): SOMEWEBSITE.COM: SOMEIPADDRESS (family 2, proto 6)
06-22 14:23:19.988: I/global(2614): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
06-22 14:23:23.048: D/dalvikvm(2614): GC freed 2144 objects / 375080 bytes in 100ms
06-22 14:23:23.148: D/dalvikvm(2614): GC freed 117 objects / 218976 bytes in 87ms
06-22 14:23:23.518: D/dalvikvm(2614): GC freed 4578 objects / 177480 bytes in 102ms
06-22 14:23:23.908: D/dalvikvm(2614): GC freed 3119 objects / 171824 bytes in 117ms
06-22 14:23:24.158: D/dalvikvm(2614): GC freed 1831 objects / 69416 bytes in 114ms
06-22 14:23:24.488: D/dalvikvm(2614): GC freed 2798 objects / 103000 bytes in 137ms
06-22 14:23:24.908: D/dalvikvm(2614): GC freed 3480 objects / 126832 bytes in 132ms
06-22 14:23:25.408: D/dalvikvm(2614): GC freed 4624 objects / 176448 bytes in 130ms
06-22 14:23:26.028: D/dalvikvm(2614): GC freed 5390 objects / 201136 bytes in 145ms
06-22 14:23:26.738: D/dalvikvm(2614): GC freed 7460 objects / 281696 bytes in 149ms
06-22 14:23:27.658: D/dalvikvm(2614): GC freed 11187 objects / 712496 bytes in 163ms
06-22 14:23:28.398: D/dalvikvm(2614): GC freed 8357 objects / 315152 bytes in 163ms
06-22 14:23:29.138: I/dalvikvm-heap(2614): Clamp target GC heap from 16.463MB to 16.000MB
06-22 14:23:29.138: D/dalvikvm(2614): GC freed 7704 objects / 289080 bytes in 162ms
06-22 14:23:30.688: I/global(2614): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
06-22 14:23:30.878: D/dalvikvm(2614): GC freed 18183 objects / 6667672 bytes in 184ms
06-22 14:23:34.768: D/dalvikvm(2614): GC freed 6048 objects / 694432 bytes in 155ms
06-22 14:23:35.428: D/dalvikvm(2614): GC freed 8257 objects / 311744 bytes in 172ms
06-22 14:23:36.068: I/dalvikvm-heap(2614): Clamp target GC heap from 16.330MB to 16.000MB
06-22 14:23:36.068: D/dalvikvm(2614): GC freed 7544 objects / 284536 bytes in 166ms
06-22 14:23:36.818: I/dalvikvm-heap(2614): Clamp target GC heap from 17.536MB to 16.000MB
06-22 14:23:36.818: D/dalvikvm(2614): GC freed 10209 objects / 482496 bytes in 188ms
06-22 14:23:36.998: I/dalvikvm-heap(2614): Clamp target GC heap from 17.631MB to 16.000MB
06-22 14:23:36.998: D/dalvikvm(2614): GC freed 4 objects / 152 bytes in 171ms
06-22 14:23:36.998: I/dalvikvm-heap(2614): Forcing collection of SoftReferences for 100180-byte allocation
06-22 14:23:37.178: I/dalvikvm-heap(2614): Clamp target GC heap from 17.631MB to 16.000MB
06-22 14:23:37.178: D/dalvikvm(2614): GC freed 0 objects / 0 bytes in 180ms
06-22 14:23:37.178: E/dalvikvm-heap(2614): Out of memory on a 100180-byte allocation.
06-22 14:23:37.178: I/dalvikvm(2614): "main" prio=5 tid=3 RUNNABLE
06-22 14:23:37.178: I/dalvikvm(2614): | group="main" sCount=0 dsCount=0 s=0 obj=0x40019680
06-22 14:23:37.178: I/dalvikvm(2614): | sysTid=2614 nice=0 sched=0/0 handle=-1095799844
06-22 14:23:37.178: I/dalvikvm(2614): at java.lang.String.<init>(String.java:~512)
06-22 14:23:37.178: I/dalvikvm(2614): at java.lang.String.copyValueOf(String.java:769)
06-22 14:23:37.178: I/dalvikvm(2614): at java.nio.CharArrayBuffer.toString(CharArrayBuffer.java:99)
06-22 14:23:37.178: I/dalvikvm(2614): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:86)
06-22 14:23:37.178: I/dalvikvm(2614): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:469)
06-22 14:23:37.178: I/dalvikvm(2614): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:147)
06-22 14:23:37.178: I/dalvikvm(2614): at dk.Activity.onCreate(Activity.java:64)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
06-22 14:23:37.188: I/dalvikvm(2614): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:23:37.188: I/dalvikvm(2614): at android.os.Looper.loop(Looper.java:123)
06-22 14:23:37.188: I/dalvikvm(2614): at android.app.ActivityThread.main(ActivityThread.java:3948)
06-22 14:23:37.188: I/dalvikvm(2614): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:23:37.188: I/dalvikvm(2614): at java.lang.reflect.Method.invoke(Method.java:521)
06-22 14:23:37.188: I/dalvikvm(2614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-22 14:23:37.188: I/dalvikvm(2614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-22 14:23:37.188: I/dalvikvm(2614): at dalvik.system.NativeStart.main(Native Method)
06-22 14:23:37.198: D/AndroidRuntime(2614): Shutting down VM
06-22 14:23:37.198: W/dalvikvm(2614): threadid=3: thread exiting with uncaught exception (group=0x40013140)
06-22 14:23:37.198: E/AndroidRuntime(2614): Uncaught handler: thread main exiting due to uncaught exception
06-22 14:23:37.208: E/AndroidRuntime(2614): java.lang.OutOfMemoryError
06-22 14:23:37.208: E/AndroidRuntime(2614): at java.lang.String.<init>(String.java:512)
06-22 14:23:37.208: E/AndroidRuntime(2614): at java.lang.String.copyValueOf(String.java:769)
06-22 14:23:37.208: E/AndroidRuntime(2614): at java.nio.CharArrayBuffer.toString(CharArrayBuffer.java:99)
06-22 14:23:37.208: E/AndroidRuntime(2614): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:86)
06-22 14:23:37.208: E/AndroidRuntime(2614): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:469)
06-22 14:23:37.208: E/AndroidRuntime(2614): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:147)
06-22 14:23:37.208: E/AndroidRuntime(2614): at dk.Activity.onCreate(Activity.java:64)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.os.Looper.loop(Looper.java:123)
06-22 14:23:37.208: E/AndroidRuntime(2614): at android.app.ActivityThread.main(ActivityThread.java:3948)
06-22 14:23:37.208: E/AndroidRuntime(2614): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:23:37.208: E/AndroidRuntime(2614): at java.lang.reflect.Method.invoke(Method.java:521)
06-22 14:23:37.208: E/AndroidRuntime(2614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-22 14:23:37.208: E/AndroidRuntime(2614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-22 14:23:37.208: E/AndroidRuntime(2614): at dalvik.system.NativeStart.main(Native Method)
06-22 14:23:37.218: I/dalvikvm(2614): threadid=7: reacting to signal 3
06-22 14:24:11.958: I/Process(2614): Sending signal. PID: 2614 SIG: 9
Problem fixed by separating all of the code into 2 Activities! (Why this works is a good question since the second Activity consists of A LOT MORE demanding code than the first Activity)The first Activity consists of the code from the top to the end of the first FOR loop.
The second Activity consists of the code from the end of the first FOR loop to the end of the remaining code.
Downgrading from 1.6.3 to 1.5.2 fixed the problem – Probably a memory leak in Jsoup 1.6.3.