【问题标题】:Background sticky concurrent mark sweep GC freed背景粘性并发标记扫描 GC 释放
【发布时间】:2017-07-11 21:52:05
【问题描述】:

我有一个应用程序,它每 10 秒检查一次数据库是否有任何新数据,如果有任何数据,它将获取它并停止检查数据库。

我已经实现了一个文本观察器来检查文本框是否为空。如果是,它将检查数据库,如果它包含任何文本,它将停止。

这是我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtBoxUser.addTextChangedListener(checkUserRent);

    getData();
}


//TEXTWATCHER FOR GETTING PERSON WHO RENTED BOX
private final TextWatcher checkUserRent = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
            check();
        } else {
            Toast.makeText(MainActivity.this, "STOP",
                    Toast.LENGTH_LONG).show();
        }
    }
};


public void start(View view)
{
    getData();
}

public void cancel(View view)
{
    txtBoxUser.setText("");
    txtBoxPasscode.setText("");
}

private void getData(){

  final String id = txtBoxName.getText().toString().trim();

    class GetEmployee extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
       //     loading = ProgressDialog.show(MainActivity.this,"Fetching...","Wait...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
      //      loading.dismiss();
            showEmployee(s);
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(DATA_URL,id);
            return s;
        }
    }
    GetEmployee ge = new GetEmployee();
    ge.execute();
}

private void showEmployee(String json){
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
        JSONObject c = result.getJSONObject(0);
        String name = c.getString(GET_BOXUSER);
        String desg = c.getString(GET_PASSCODE);

        txtBoxUser.setText(name);
        txtBoxPasscode.setText(desg);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}


private void check()
{
    getData();

}

但是我在 logcat 等待数据时得到了这个。我只是想知道它是否可以或安全?

I/art: Background sticky concurrent mark sweep GC freed 5614(449KB) AllocSpace objects, 18(288KB) LOS objects, 33% free, 1691KB/2MB, paused 5.354ms total 10.731ms
I/art: Background sticky concurrent mark sweep GC freed 7039(557KB) AllocSpace objects, 22(352KB) LOS objects, 39% free, 1561KB/2MB, paused 10.554ms total 15.931ms 
I/art: Background partial concurrent mark sweep GC freed 7279(564KB) AllocSpace objects, 21(336KB) LOS objects, 40% free, 1504KB/2MB, paused 5.721ms total 15.823ms
I/art: WaitForGcToComplete blocked for 5.375ms for cause HeapTrim
I/art: Background partial concurrent mark sweep GC freed 7650(591KB) AllocSpace objects, 22(352KB) LOS objects, 40% free, 1505KB/2MB, paused 5.511ms total 21.465ms

【问题讨论】:

    标签: android performance garbage-collection


    【解决方案1】:

    这很好。这意味着您正在使用内存,然后它被 GC 释放。唯一的问题是您的内存不足,或者您看到由于垃圾收集而导致的性能问题。但它不需要你竞相修复。

    【讨论】:

    • 那么,解决这个问题的方法是什么,我在编辑文本框时也面临同样的情况???
    • @GeorgySavatkov 这不是问题,所以没有什么可解决的。一些算法/应用程序只使用内存。如果没有性能问题,那很好
    • 我的应用程序挂起几秒钟,因为发生了这种情况。请提出解决方案。
    【解决方案2】:

    我也遇到了同样的问题。您的代码可以运行,但如果挂起时间过长,应用程序可能会收到应用程序无响应错误并关闭。它也违背了使用 AsyncTask 的目的。

    问题是您的 AsyncTask 包含一个文本视图,因此它会在执行时阻止任何其他操作。要解决此问题,请将您的 AsyncTask 设为静态并将其传递给 Text 视图的引用。然后将其存储在 WeakReference 中。

    static class GetEmployee extends AsyncTask<Void,Void,String>{
    
        WeakReference<TextView> textUserWeakReference;
        WeakReference<TextView> textPassWeakReference;
        GetEmployee(TextView textUser, TextView textPass) {
            textUserWeakReference = new WeakReference<>(textUser);
            textPassWeakReference = new WeakReference<>(textPass);
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
       //     loading = ProgressDialog.show(MainActivity.this,"Fetching...","Wait...",false,false);
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
      //      loading.dismiss();
            TextView textUser = textUserWeakReference.get();
            TextView textPass = textPassWeakReference.get();
            if(textView != null && textPass != null)
                showEmployee(s, textUser, textPass);
        }
    
        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(DATA_URL,id);
            return s;
        }
    }
    private static void showEmployee(String json, TextView txtBoxUser, TextView txtBoxPassCode){
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
        JSONObject c = result.getJSONObject(0);
        String name = c.getString(GET_BOXUSER);
        String desg = c.getString(GET_PASSCODE);
    
        txtBoxUser.setText(name);
        txtBoxPasscode.setText(desg);
    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,它是由无限循环调用的。这让我的应用崩溃了。

      我找到了无限循环,删除了它,现在应用程序没有崩溃,我也没有这些日志了。

      【讨论】:

        猜你喜欢
        • 2016-07-30
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 2018-11-21
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        相关资源
        最近更新 更多