1

For some reason, This class is doing too much work and lags which is a major issue in the context of my app. When a user clicks on a location on the map, it should get the address of that location quickly but unfortunately I get logCat dalvikvm paused, memory freed messages and thread doing too much work on application. Not sure why. I close db connections after they are opened.

private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {

    @Override
    protected List<Address> doInBackground(String... locationName) {
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // get maximum of 3 addresses that match input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {

        if (addresses == null || addresses.size() == 0) {
            Toast.makeText(getApplicationContext(), "No location found",
                    Toast.LENGTH_SHORT).show();
        }

        // clear existing markers on map
        map.clear();

        // add markers on map for each matching address
        for (int i = 0; i < addresses.size(); i++) {
            Address address = (Address) addresses.get(i);

            // creating an instance of geoPoint to display in googleMap
            latLng = new LatLng(address.getLatitude(),
                    address.getLongitude());

            String addressText = String.format(
                    "%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address
                            .getCountryName());



            markerOp = new MarkerOptions();
            markerOp.position(latLng);
            markerOp.title(addressText);

            map.addMarker(markerOp);
            // Locate the first location
            if (i == 0)
                map.animateCamera(CameraUpdateFactory.newLatLng(latLng));

        }

    }

}

/**
 * Locates address using LatLng coordinates
 * @author Aaron
 *
 */
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
    Context mContext;

    public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;

    }

    // Finding address using reverse geocoding
    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;

        List<Address> addresses = null;
        String addressText = "";

        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 3);
            Thread.sleep(500);
        } catch (IOException e) {
            e.printStackTrace();

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

        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);

            addressText = String.format(
                    "%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address.getLocality(),
                    address.getCountryName());

        }

        return addressText;
    }

    @Override
    protected void onPostExecute(String addressText) {
        // Setting the title for the marker.
        // This will be displayed on taping the marker
        markerOp.title(addressText);

        // Placing a marker on the touched position
        map.addMarker(markerOp);

    }

}

And Logcat produces

03-04 16:47:38.037: I/Choreographer(16424): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-04 16:47:38.287: W/ActivityThread(16424): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
03-04 16:47:38.407: D/dalvikvm(16424): GC_FOR_ALLOC freed 1017K, 14% free 11702K/13476K, paused 48ms, total 49ms
03-04 16:47:38.637: D/dalvikvm(16424): GC_FOR_ALLOC freed 1052K, 12% free 11965K/13476K, paused 25ms, total 25ms
03-04 16:47:38.907: D/dalvikvm(16424): GC_FOR_ALLOC freed 1101K, 9% free 12300K/13476K, paused 25ms, total 25ms
03-04 16:47:39.277: D/dalvikvm(16424): GC_FOR_ALLOC freed 1330K, 10% free 12526K/13892K, paused 28ms, total 28ms
03-04 16:47:39.537: D/dalvikvm(16424): GC_FOR_ALLOC freed 1486K, 11% free 12667K/14192K, paused 36ms, total 36ms
03-04 16:47:39.757: D/dalvikvm(16424): GC_FOR_ALLOC freed 1324K, 10% free 13025K/14384K, paused 32ms, total 32ms
03-04 16:47:40.037: D/dalvikvm(16424): GC_FOR_ALLOC freed 1774K, 13% free 13039K/14852K, paused 52ms, total 55ms
03-04 16:47:40.107: D/dalvikvm(16424): GC_FOR_ALLOC freed 29K, 12% free 13178K/14852K, paused 27ms, total 27ms
03-04 16:47:40.337: D/dalvikvm(16424): GC_FOR_ALLOC freed 906K, 11% free 13477K/15040K, paused 32ms, total 33ms
03-04 16:47:40.417: I/dalvikvm(16424): Jit: resizing JitTable from 4096 to 8192
03-04 16:47:40.607: D/dalvikvm(16424): GC_FOR_ALLOC freed 1098K, 14% free 13388K/15448K, paused 28ms, total 28ms
03-04 16:47:42.687: D/dalvikvm(16424): GC_FOR_ALLOC freed 1307K, 14% free 13429K/15448K, paused 30ms, total 30ms
03-04 16:47:42.687: I/dalvikvm-heap(16424): Grow heap (frag case) to 14.147MB for 1048592-byte allocation
03-04 16:47:42.717: D/dalvikvm(16424): GC_FOR_ALLOC freed 5K, 13% free 14448K/16476K, paused 28ms, total 28ms
03-04 16:47:42.787: D/dalvikvm(16424): GC_FOR_ALLOC freed 1073K, 19% free 13418K/16476K, paused 29ms, total 29ms
03-04 16:47:43.217: D/dalvikvm(16424): GC_FOR_ALLOC freed 1550K, 19% free 13425K/16476K, paused 29ms, total 29ms
03-04 16:47:43.527: D/dalvikvm(16424): GC_FOR_ALLOC freed 1657K, 17% free 13685K/16476K, paused 30ms, total 30ms
03-04 16:47:44.337: D/dalvikvm(16424): GC_FOR_ALLOC freed 1620K, 15% free 14084K/16476K, paused 34ms, total 34ms
03-04 16:47:46.547: D/dalvikvm(16424): GC_FOR_ALLOC freed 2071K, 15% free 14140K/16476K, paused 37ms, total 37ms
03-04 16:47:52.147: D/dalvikvm(16424): GC_FOR_ALLOC freed 2585K, 18% free 13636K/16476K, paused 38ms, total 39ms
03-04 16:47:54.737: D/dalvikvm(16424): GC_FOR_ALLOC freed 1834K, 17% free 13804K/16476K, paused 48ms, total 48ms
03-04 16:48:05.387: D/dalvikvm(16424): GC_FOR_ALLOC freed 2096K, 17% free 13766K/16476K, paused 50ms, total 50ms
03-04 16:48:51.197: D/dalvikvm(16424): GC_FOR_ALLOC freed 2062K, 17% free 13750K/16476K, paused 43ms, total 46ms
03-04 16:48:55.627: D/dalvikvm(16424): GC_FOR_ALLOC freed 2033K, 17% free 13757K/16476K, paused 40ms, total 40ms
03-04 16:49:45.877: D/dalvikvm(16424): GC_FOR_ALLOC freed 2055K, 17% free 13745K/16476K, paused 41ms, total 42ms
03-04 16:50:31.287: D/dalvikvm(16424): GC_FOR_ALLOC freed 2041K, 17% free 13744K/16476K, paused 38ms, total 38ms
03-04 16:51:21.397: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 37ms, total 37ms
03-04 16:52:06.817: D/dalvikvm(16424): GC_FOR_ALLOC freed 2040K, 17% free 13744K/16476K, paused 38ms, total 38ms
03-04 16:52:56.817: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 39ms, total 39ms
03-04 16:53:42.307: D/dalvikvm(16424): GC_FOR_ALLOC freed 2039K, 17% free 13744K/16476K, paused 39ms, total 39ms
03-04 16:54:32.277: D/dalvikvm(16424): GC_FOR_ALLOC freed 2038K, 17% free 13744K/16476K, paused 90ms, total 90ms
user2219097
  • 357
  • 3
  • 8
  • 25

1 Answers1

1

Keep in mind that onPostExecute gets run on the UI thread and yours seems quite hefty, running through a loop that creates multiple objects. With this in mind I think you should try make your onPostExecute method in your GeocoderTask more lightweight. Consider doing all of the processing in your doInBackground method and then only call map.animateCamera(CameraUpdateFactory.newLatLng(latLng)) in onPostExecute()

Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30
  • 1
    You should also read about `String` and `immutable` and consider doing something other than creating lots of strings, which are objects, in your loop. – Simon Mar 04 '14 at 17:37
  • ironically its not laggy in that postExecution but the one for ReverseGeocoding yet not much is in that method however. Any ideas why? – user2219097 Mar 04 '14 at 17:44
  • Well onPostExecute only gets called after all the loading in background is finished, so your doInBackground method in the ReverseGeocodingTask must be taking a while. – Kent Hawkings Mar 05 '14 at 08:06