mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

Memory leak in Android?

Open zimbricchio opened this issue 10 years ago • 4 comments

Platform: Android 6 Mapbox SDK version: 4.1.1

I made a small app to reproduce issue #6881. The app switches continuously between two activities. One activity contains a map, the other is empty.

The map activity initializes the map and then starts a timer that starts the empty activity. The empty activity has a similar timer switching back to the map activity.

Once started, the app crashes after a dozen cycles. It seems like a memory leak. See logcat.

map activity class (overrides of other methods of Activity that call mapView were omitted)

    private MapView mapView;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapboxAccountManager.start(this, yourMapBoxAccessToken);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
        if(timer != null) {
            timer.cancel();
        }
        timer = new Timer(true);
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        startActivity(new Intent(MapActivity.this, EmptyActivity.class));
                                    }
                                }
                        );
                    }
                },
                500
        );
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
        if(timer != null) {
            timer.cancel();
        }
    }

map activity layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ~~mapbox:style_url="@string/style_mapbox_streets"~~
    />
</RelativeLayout>

empty activity class

    @Override
    protected void onResume() {
        super.onResume();
        if(timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new Timer(true);
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        startActivity(new Intent(EmptyActivity.this, MapActivity.class));
                                    }
                                }
                        );
                    }
                },
                500
        );
    }


    @Override
    protected void onPause() {
        super.onPause();
        if(timer != null) {
            timer.cancel();
            timer = null;
        }
    }

logcat

11-07 11:32:47.601 7458-7458/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.mapboxtest, PID: 7458
java.lang.OutOfMemoryError: Could not allocate JNI Env
at java.lang.Thread.nativeCreate(Native Method)
at java.lang.Thread.start(Thread.java:1063)
at java.util.Timer$TimerImpl.<init>(Timer.java:192)
at java.util.Timer.<init>(Timer.java:365)
at java.util.Timer.<init>(Timer.java:384)
at com.test.mapboxtest.MapActivity.onResume(MapActivity.java:35)

zimbricchio avatar Nov 04 '16 16:11 zimbricchio

@zimbricchio

switching back to the map activity

Are you calling Activity#finish() or creating a new Intent?

tobrun avatar Nov 07 '16 11:11 tobrun

create a new intent: startActivity(new Intent(EmptyActivity.this, MapActivity.class));

zimbricchio avatar Nov 07 '16 12:11 zimbricchio

@zimbricchio First I want to point out that java.lang.OutOfMemoryError is not exactly the same as an Memory leak. In the former you are allocating too much memory and thus throwing the exception while in the latter you are not correctly allowing garbage collection to occur thus leaking memory. Offcourse it's valid to assume a memory leak could trigger an OutOfMemory. Note that we are using LeakCanary to trace memory leaks. Did this framework report anything?

Secondly, you are creating a new Intent Object on the second Activity. This will add an activity on top of the current one instead of "returning" (for this you need to call Activity#finish). This means you are app has multiple instances of our SDK components active in heap. Every startup of an Activity will allocate more memory and the other activities in the stack remain paused and aren't being recylced. Both these items contribute to throwing an out of memory.

Would you be able to trigger the exception if you would use finish instead?

tobrun avatar Nov 07 '16 12:11 tobrun

Yes of course, the memory leak was only an assumption.

I tried with Activity#finish and seems stable now.

zimbricchio avatar Nov 07 '16 13:11 zimbricchio