Heavy thread load causing memory problems

In the logarithm, I see this all the time:

I / art: background sticky parallel sweep of GC tag released 141468 (7 MB) AllocSpace objects, LOS 3 objects (255 KB), 25% free, 21 MB / 29 MB, suspended for 1.228ms in total 132.652ms

and this:

W / art: Pausing all threads takes: xxx milliseconds

And that makes me get org.apache.http.conn.HttpHostConnectException. Connecting to https://plcloud.c6.ixsecure.com has abandoned the last web service method that I am doing.

How can I stop “pausing all threads” and using large garbage collection so that I can go through all the calls to the web service methods without memory problems?

Currently, it receives data from a web service and stores it in a local SQLite database. Here is my code:

Code that calls the get methods of the synchronization classes (which both receive data from the web service and store it in the sqlite database):

class SyncThread extends Thread {
    @Override
    public void run() {
        try {
            syncHandler.sendEmptyMessage(16);
            Credentials credentials = new Credentials(usernameField
                    .getText().toString(), passwordField.getText()
                    .toString());
            UserCredentialsOut userCredentials = credentials
                    .getCredentials();

            if (userCredentials.getReturnCode() == 1) {
                Settings.getInstance().setEntityId(
                        userCredentials.getEntityId());

                syncHandler.sendEmptyMessage(1);
                CustomerSync custSync = new CustomerSync(context,
                        syncProgressDialog);
                Log.i("Customer Sync", "getCustomers");
                custSync.getCustomers(credentials);

                syncHandler.sendEmptyMessage(2);
                PetSync petSync = new PetSync(context, syncProgressDialog);
                Log.i("Pet Sync", "getPets");
                petSync.getPets(credentials);

                syncHandler.sendEmptyMessage(4);
                SystemSync systemSync = new SystemSync(context,
                        syncProgressDialog);
                Log.i("ListValues Sync", "getListValuesInitials");
                systemSync.getListValuesInitials(credentials);

                syncHandler.sendEmptyMessage(5);
                Log.i("Vets Sync", "getVets");
                systemSync.getVets(credentials);

                syncHandler.sendEmptyMessage(6);
                Log.i("Groomers Sync", "getGroomers");
                try{
                    systemSync.getGroomers(credentials);
                }catch (NullPointerException e) {
                    Log.e("NullPointerException", e.getMessage()+"benebne");
                }

                syncHandler.sendEmptyMessage(7);
                Log.i("Services Sync", "getServices");
                systemSync.getServices(credentials);

                syncHandler.sendEmptyMessage(8);
                Log.i("CalendarDays Sync", "getCalendarDays");
                systemSync.getCalendarDays(credentials);

                syncHandler.sendEmptyMessage(10);
                Log.i("loyeeUnavailTimes Sync",
                        "getEmployeeUnavailTimes");
                systemSync.getEmployeeUnavailTimes(credentials);

                syncHandler.sendEmptyMessage(12);
                Log.i("Runs Sync", "getRuns");
                systemSync.getRuns(credentials);

                syncHandler.sendEmptyMessage(14);
                Log.i("DaycareGroups Sync", "getDaycareGroups");
                systemSync.getDaycareGroups(credentials);

                syncHandler.sendEmptyMessage(3);
                OptionsSync optSync = new OptionsSync(context,
                        syncProgressDialog);
                Log.i("SystemOptions Sync", "getSystemOptions");
                SystemOptions systemOptions = optSync
                        .getSystemOptions(credentials);

                if (systemOptions != null) {

                    if (systemOptions.isModuleGroom()) {
                        syncHandler.sendEmptyMessage(9);
                        PetGroomSync petGroomSync = new PetGroomSync(
                                context, syncProgressDialog);
                        Log.i("PetGrooms Sync", "getPetGrooms");
                        petGroomSync.getPetGrooms(credentials);

                        syncHandler.sendEmptyMessage(11);
                        AppointmentSync appointmentSync = new AppointmentSync(
                                context, syncProgressDialog);
                        Log.i("Appointments Sync", "getAppointments");
                        appointmentSync.getAppointments(credentials);
                    }

                    if (systemOptions.isModuleBoard()) {
                        syncHandler.sendEmptyMessage(13);
                        AppointmentBoardSync appointmentBoardSync = new AppointmentBoardSync(
                                context, syncProgressDialog);
                        Log.i("ApptBoards Sync", "getApptBoards");
                        appointmentBoardSync.getApptBoards(credentials);
                    }

                    if (systemOptions.isModuleDaycare()) {
                        syncHandler.sendEmptyMessage(15);
                        AppointmentDaycareSync appointmentDaycareSync = new AppointmentDaycareSync(
                                context, syncProgressDialog);
                        Log.i("ApptDayCares Sync", "getApptDayCares");
                        appointmentDaycareSync.getApptDaycares(credentials);
                    }
                }

                syncHandler.sendEmptyMessage(0);
                return;
            } else {
                syncHandler.sendMessage(syncHandler.obtainMessage(-1,
                        userCredentials.getMessage()));
                return;
            }

        } catch (ClientProtocolException e) {
            Log.e("ClientProtocolException", e.toString());
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        } catch (Exception e) {
            Log.e("SyncException", e.toString());
        }

        syncHandler.sendMessage(syncHandler.obtainMessage(-1,
                getString(R.string.connError)));
    }
}

One actual synchronization class - all synchronization classes are approximately the same, so you only need to see one of them - pay attention to the getCustomers method, which calls "initCustomersFromJson" - these are the parts that receive the data and then save it to sqlite db:

public class CustomerSync implements ICustomerSync {
    public static final int QUANTITY = 0;
    private long offset;
    private ProgressDialog progressDialog;
    private Context context;
    private HttpClient client;
    private final String HTTPS_GET_CUSTOMERS = "https://plcloud.c6.ixsecure.com/PLService.svc/GetCustomers";
    private final String GET_URL = "{0}?quant={1}&offset={2}";
    private final String HTTPS_SYNC_CUSTOMERS = "https://plcloud.c6.ixsecure.com/PLService.svc/SyncCustomers";

    private CustomerSync() {
        client = new DefaultHttpClient();
    }

    public CustomerSync(Context context, ProgressDialog progressDialog) {
        this();
        this.context = context;
        this.progressDialog = progressDialog;
    }

    public Customer[] initCustomersFromJson(Credentials credentials)
            throws ClientProtocolException, IOException, URISyntaxException {
        String getUri;
        getUri = MessageFormat.format(GET_URL, HTTPS_GET_CUSTOMERS,
                QUANTITY + "", offset + "");

        credentials.initGetOAuthStructure(HTTPS_GET_CUSTOMERS);

        HttpGet request = new HttpGet(getUri + "&"
                + credentials.getOauthStructure());
        String content = client.execute(request, new BasicResponseHandler());

        Gson gson = new GsonBuilder().serializeNulls()
                .excludeFieldsWithoutExposeAnnotation().create();
        return gson.fromJson(content, Customer[].class);
    }

    @Override
    public void getCustomers(Credentials credentials)
            throws ClientProtocolException, IOException, URISyntaxException {
        ICustomerDAO customerDAO = (ICustomerDAO) DAOFactory.getDAO(
                ICustomerDAO.class.getName(), context);
        customerDAO.open();

        Customer[] customers;


            customers = initCustomersFromJson(credentials);
            progressDialog.setMax(customers.length);
            progressDialog.setProgress(0);
            customerDAO.saveCustomers(customers, progressDialog);

            if (customers.length > 0) {
                offset = customers[customers.length - 1].getId();
            }


        customerDAO.close();

        Settings.getInstance().setLastSyncCustomerDatetime(new Date());
    }
+4
source share
1 answer

I think it’s better to transfer data if the data is really big. I see in your code you are already using GSON, and you can use GSON to stream json data.

:

        String getUri;
        getUri = MessageFormat.format(GET_URL, HTTPS_GET_CUSTOMERS,
                QUANTITY + "", offset + "");

        credentials.initGetOAuthStructure(HTTPS_GET_CUSTOMERS);

        HttpGet request = new HttpGet(getUri + "&"
                + credentials.getOauthStructure());
        HttpResponse response = client.execute(request);

        Reader streamReader = new InputStreamReader(response
                    .getEntity().getContent());
        JsonReader reader = new JsonReader(streamReader);
        ArrayList<Customer> customerList = new ArrayList<Customer>();
        reader.beginArray();
        while (reader.hasNext()) {
            Customer customer = new Customer();
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("Action")) {
                    customer.setAction(reader.nextString());
                } else if (name.equals("Addr1")) {
                    customer.setAddr1(reader.nextString());
                } else if (name.equals("Addr2")) {
                    customer.setAddr2(reader.nextString());
                } else if (//Other condition and value)
                    ...
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
            customerList.add(customer);
        }
        reader.endArray();
        reader.close();

JsonReader , , , .

+1

All Articles