如果我在我的 HTC 魔术手机上运行以下代码,我会收到java.lang.OutOfMemoryError
错误消息。对于我的手机来说,这个简单的短代码要求太多了吗?
我认为代码很简单而且很有效,但我的手机根本吃不下它。如果我将代码切成一半(就在第一个for
循环之后),手机就会吃掉它。
我检查了我的手机内存,在“打开高级任务杀手”应用程序的帮助下,它显示 79 MB 可用。
我没有同时打开很多应用程序,而且我尝试执行的代码并不多(在我看来)。
我真的必须买一部新手机还是有其他方法可以解决这个问题?
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());
}
}
}
这只是我真正想要执行的代码总量的 1/15
日志猫:
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