gpt4 book ai didi

android - Android 中用于 AsyncTask 的过多垃圾收集器

转载 作者:行者123 更新时间:2023-11-29 17:18:08 24 4
gpt4 key购买 nike

我正在尝试使用 URL 下载 json 对象,为此我创建了一个 AsyncTask。每当我启动我的应用程序时,都会启动过多的垃圾收集器。我无法理解我哪里出错了。我没有执行任何繁重的内存使用任务,只是试图为 json 响应调用 api。

主要 Activity 代码:

public class MainActivity extends AppCompatActivity {


public class DownloadTask extends AsyncTask<String,Void,String>{

@Override
protected String doInBackground(String... urls) {

String result=null;
URL url;
HttpURLConnection httpURLConnection=null;

try {
url= new URL(urls[0]);
httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream= httpURLConnection.getInputStream();
InputStreamReader inputStreamReader= new InputStreamReader(inputStream);
int data= inputStreamReader.read();
while(data !=1){
char current= (char) data;
result +=current;
data= inputStreamReader.read();
}
return result;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("webssite Content :",result);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask downloadTask= new DownloadTask();
downloadTask.execute("http://api.openweathermap.org/data/2.5/weather?q=London&appid=5868b18238384fb4bdb3123jn9f0847cc");
}}

逻辑猫

06-19 08:45:57.149 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 3ms, total 3ms
06-19 08:45:57.149 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720030-byte allocation
06-19 08:45:57.149 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 6ms, total 6ms
06-19 08:45:57.159 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 5ms, total 5ms
06-19 08:45:57.159 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 3ms, total 3ms
06-19 08:45:57.159 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720034-byte allocation
06-19 08:45:57.159 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 3ms, total 3ms
06-19 08:45:57.169 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 3ms, total 3ms
06-19 08:45:57.169 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 2ms, total 2ms
06-19 08:45:57.169 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720036-byte allocation
06-19 08:45:57.179 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 4ms, total 4ms
06-19 08:45:57.179 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 3ms, total 3ms
06-19 08:45:57.189 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 7ms, total 7ms
06-19 08:45:57.189 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720040-byte allocation
06-19 08:45:57.189 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 3ms, total 3ms
06-19 08:45:57.199 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 2ms, total 2ms
06-19 08:45:57.199 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 3ms, total 3ms
06-19 08:45:57.199 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720042-byte allocation
06-19 08:45:57.199 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 3ms, total 3ms
06-19 08:45:57.209 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 3ms, total 3ms
06-19 08:45:57.219 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 5ms, total 5ms
06-19 08:45:57.219 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720046-byte allocation
06-19 08:45:57.229 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 4ms, total 4ms
06-19 08:45:57.229 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 2ms, total 2ms
06-19 08:45:57.239 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 5ms, total 5ms
06-19 08:45:57.239 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720048-byte allocation
06-19 08:45:57.239 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 4ms, total 4ms
06-19 08:45:57.249 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 4ms, total 4ms
06-19 08:45:57.249 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 5ms, total 5ms
06-19 08:45:57.249 6269-6283/com.hemkar.jsondemo I/dalvikvm-heap: Grow heap (frag case) to 5.091MB for 720052-byte allocation
06-19 08:45:57.259 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 468K, 24% free 4672K/6144K, paused 5ms, total 5ms
06-19 08:45:57.259 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed 1172K, 36% free 3969K/6144K, paused 3ms, total 3ms
06-19 08:45:57.269 6269-6283/com.hemkar.jsondemo D/dalvikvm: GC_FOR_ALLOC freed <1K, 28% free 4438K/6144K, paused 8ms, total 8ms
06-19 0

最佳答案

我认为因为在您的代码中while(data !=1),正确的应该是while(data !=-1)。循环是无限的

关于android - Android 中用于 AsyncTask 的过多垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com