我想知道为什么我的应用程序无法在我朋友的手机上运行。 我已经检查了我的应用程序的RAM使用情况,我发现它需要~180Mb才能加载所有位图。所以我几乎没有问题:
答案 0 :(得分:0)
我已经检查了我的应用程序的RAM使用情况,我发现它需要~180Mb才能加载所有位图。
在许多设备上,如果不是大多数设备,您的应用程序无权拥有180MB的堆空间。在设备固件中设置了每个应用程序堆限制。这可能低至16MB,但除了最低端的设备外,其他所有设备都是32-64MB。但180MB会非常大。
虽然您的应用可能能够拥有~180MB堆(例如android:largeHeap
),但仍有大量设备可供您使用堆空间。
请注意,堆限制与设备上当前可用RAM的数量无关。如果手机有大约13MB的可用内存,大约130MB的可用内存,或者~1.3GB的可用内存,则无关紧要 - 堆限制是堆限制。
我强烈建议您减少堆使用量,理想情况是减少10倍左右。
是否可以在手机上运行需要180Mb RAM的应用程序,在运行时有~130Mb可用内存?
可能。假设您的应用程序当时位于前台,Android将终止其他进程以释放系统RAM。这可能包括终止某些用户希望不终止的流程。
恕我直言,这太过分了。180Mb的RAM使用率,加载少量图像(150kb一个)是可以还是太多?
答案 1 :(得分:0)
是的150kb图像太多了....你需要调整它的大小,以便android根据需要分配内存。它可以防止OOM应用程序崩溃。
int resolution=100;
profileImageViewNav.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.drawable.profile_image, resolution, resolution));
这是decodeSampledBitmapFromResource()函数
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
这是calculateInSampleSize()函数代码
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}