我有一个带有5-6标记的MapsActivity。我的客户要求每2分钟刷新一次活动。刷新几次后,会发生OutOfMemoryError。检查Android Profiler时,我注意到每次刷新活动时,垃圾收集器都会破坏活动的旧实例。
这是代码......
//for draw child user location dynamically
private void drawDynamicMap(double latitude, double longitude, final String title,
final String groupDatetime, String status, String type, GoogleMap googleMap) {
if (latitude == 0.0 && longitude == 0.0) {
// return;
}
mMap = googleMap;
final LatLng cordinate = new LatLng(latitude, longitude);
//for setting image marker
View marker = getLayoutInflater()
.inflate(R.layout.custom_marker_layout, null);
View carMarker = getLayoutInflater()
.inflate(R.layout.custom_car_marker_layout, null);
// Dashboard Button Work
ImageView markerImage = (ImageView) marker.findViewById(R.id.marker_image);
/*NavigationActionbar.*/
if (!image.equals("")) {
Picasso.with(getApplicationContext())
.load(image) /// if load from JSON .load(responseJSON.getString("image"))
.placeholder(R.drawable.noimage)
.error(R.drawable.noimage)
.resize(80, 80)
.into(markerImage);
}
if (mMap == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
refreshMapActivity();
}
childMarker = mMap.addMarker(new MarkerOptions()
.position(cordinate)
.title(title)
.snippet(status + ", On - " + groupDatetime));
if (type.equals("APP")) {
childMarker.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(marker)));
} else {
childMarker.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(carMarker)));
}
childMarkers.add(childMarker);
}
这是logcat中的错误...
java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 1081744 free bytes and 1056KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
at com.google.maps.api.android.lib6.gmm6.vector.gl.k.a(:com.google.android.gms.DynamiteModulesB@11951248:7)
at com.google.maps.api.android.lib6.gmm6.vector.gl.ah.a(:com.google.android.gms.DynamiteModulesB@11951248:73)
at com.google.maps.api.android.lib6.gmm6.vector.gl.ah.a(:com.google.android.gms.DynamiteModulesB@11951248:46)
at com.google.maps.api.android.lib6.gmm6.vector.gl.ah.b(:com.google.android.gms.DynamiteModulesB@11951248:39)
at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.ae.a(:com.google.android.gms.DynamiteModulesB@11951248:59)
at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.am.a(:com.google.android.gms.DynamiteModulesB@11951248:346)
at com.google.maps.api.android.lib6.gmm6.vector.cb.a(:com.google.android.gms.DynamiteModulesB@11951248:407)
at com.google.maps.api.android.lib6.gmm6.vector.bs.a(:com.google.android.gms.DynamiteModulesB@11951248:664)
at com.google.maps.api.android.lib6.gmm6.vector.bs.b(:com.google.android.gms.DynamiteModulesB@11951248:270)
at com.google.maps.api.android.lib6.gmm6.vector.bb.f(:com.google.android.gms.DynamiteModulesB@11951248:257)
at com.google.maps.api.android.lib6.gmm6.vector.bb.run(:com.google.android.gms.DynamiteModulesB@11951248:11)
注意:有时我的设备挂起并强行重启。目前我正在处理内存泄漏问题,并且已成功消除了上下文和内部类的一些泄漏。
我怀疑错误与Bitmap有关,这就是为什么我把上面的代码。如果需要任何其他细节,请ping我。
但我不知道如何解决这个问题。我希望,如果有人在过去遇到同样的问题。