Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Google Maps v2 big memory consumption
    text
    copied!<p>I have an application which crashes after using it for some time on a tablet with <code>OutOfMemoryError</code>. My application uses master-details layout with <code>Fragments</code>. After doing a lot of debugging I came to the conclusion that big memory consumption is caused by maps which are used in the details view. </p> <p>I created an example as simple as I could which may lack many important features but demonstrates the problem.</p> <p>TestMapActivity:</p> <pre><code>public class TestMapActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_map_activity); } public void newMap(View view) { getSupportFragmentManager().beginTransaction().replace(R.id.mapFrame, new TestMapFragment()).addToBackStack(null).commit(); } } </code></pre> <p>Activity's layout file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="newMap" android:text="new map" /&gt; &lt;FrameLayout android:id="@+id/mapFrame" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>TestMapFragment:</p> <pre><code>public class TestMapFragment extends Fragment { protected MapView mMapView; protected GoogleMap mMap; @Override public void onSaveInstanceState(Bundle outState) { mMapView.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMapView = new MapView(REdroid.sContext); mMapView.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.test_map_fragment, container, false); ((FrameLayout) v.findViewById(R.id.mapFrame)).addView(mMapView); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setUpMapIfNeeded(); } @Override public void onResume() { super.onResume(); mMapView.onResume(); } @Override public void onPause() { mMapView.onPause(); super.onPause(); } @Override public void onDestroy() { mMapView.onDestroy(); super.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); mMapView.onLowMemory(); }; private final void setUpMapIfNeeded() { if (mMap == null) { mMap = mMapView.getMap(); if (mMap != null) { setUpMap(); } } } protected void setUpMap() { try { MapsInitializer.initialize(getActivity()); } catch (GooglePlayServicesNotAvailableException e) { } } } </code></pre> <p>Fragment's layout file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapFrame" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;/FrameLayout&gt; </code></pre> <p>I'm launching the activity and pressing the button to show the map. When I'm checking memory consumption with DDMS I can see that each time I press "new map" the "Allocated" memory grows by around 0.5MB. As you can imagine It's really a problem when user watches many items (which is quite normal behaviour in my app) and the app takes 5MB each 10 items (actually in the real app it takes 1MB for each view).</p> <p>Is this normal? What can I do to stop the app from using this much memory? I read here about Google Maps having some memory leaks but can they be this big?</p> <p><strong>EDIT</strong></p> <p>I corrected the TestMapActivity.newMap() function. I added adding transaction to the stack because the memory "leak" doesn't occur if I the operation isn't put to backstack.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload