0

I wrote this program to overlap circles to show where I've been using the google maps android api v2. After about 2 hours I get the java.lang.OutOfMemoryError with the heap size being larger than allocated. From stack trace... (Heap Size=98304KB, Allocated=86536KB)

Why does this take up so much memory to store all these overlapping circles?

public void onLocationChanged(Location location) {

     CircleOptions circleOptions = new CircleOptions()
        .center(new LatLng(location.getLatitude(), location.getLongitude()));

    circleOptions.radius(10); // In meters

          mMap.addCircle(circleOptions);

}

Any suggestions on how to mitigate this? I would like to keep the circles so it isn't an option to use map.clear(). Also I need to keep using the circles because it allows me to set a path width on the map.

johnsonjp34
  • 3,139
  • 5
  • 22
  • 48
  • how often do you think `onLocationChanged` is called? How many circles do you think you have after 2 hours? do you recycle them or reuse them? do you turn off the listener when the device has the screen off? – gian1200 Jan 27 '14 at 04:47

2 Answers2

0

The location manager can take the parameters:

provider        the name of the provider with which to register
minTime         minimum time interval between location updates, in milliseconds
minDistance     minimum distance between location updates, in meters
listener        a LocationListener whose onLocationChanged(Location) method will be called for each location update

locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 0, this);

Increasing the minimum time interval or distance between location updates will provide less circles on the map, thereby increasing the amount of time the program can be run before reaching the maximum heap size.

johnsonjp34
  • 3,139
  • 5
  • 22
  • 48
0

You need to optimize it. Maybe you are drawing circles at the same coordinates. Each circle you draw is a memory allocation, so try to see if the onLocationChanged is called with the same locations some times.

Renan Bandeira
  • 3,238
  • 17
  • 27