앱이 사용할 수있는 최대 RAM 양은 얼마입니까?
Android 운영 체제 의 메모리 관리 와 관련 하여이 질문에 대해 궁금한 점이 있으므로 해당 주제에 대한 자세한 답변을 바랍니다.
내가 알고 싶은 것 :
- Android 애플리케이션 (시스템 앱이 아님)이 사용할 수 있는 최대 메모리 양 ( MB / 총 RAM의 백분율 )은 얼마입니까?
- 안드로이드 버전 간에 차이가 있습니까?
- 장치 제조업체 와 관련하여 차이가 있습니까?
그리고 가장 중요한 것은 :
- 무엇 간주됩니다 / 가에 의존하는 일 이 응용 프로그램 실행시에 사용할 수있는 RAM의 양을 결정하는 시스템에 올 때 (응용 프로그램 당 메모리의 최대 정적 번호가 아닙니다 가정)?
내가 지금까지들은 내용 (2013 년까지) :
- 초기 Android 기기의 앱당 한도는 16MB입니다.
- 나중에이 한도는 24MB 또는 32MB로 증가했습니다.
나를 매우 궁금하게 만드는 것은 :
이 두 한계는 매우 낮습니다.
최근에 장치 작업 RAM을 확인하기 위해 Android 작업 관리자 를 다운로드했습니다 . 내가 주목 한 것은 약 40-50 메가 바이트의 RAM을 사용하는 응용 프로그램이 있다는 것입니다.이 응용 프로그램은 언급 된 최대 RAM 사용량보다 32MB라고합니다. 그렇다면 Android는 앱이 사용할 수있는 RAM의 양을 어떻게 결정합니까? 앱이 한도를 초과 할 수 있습니까?
또한 약 30-40 메가 바이트를 사용할 때 OutOfMemoryException이 발생하는 일부 응용 프로그램이 충돌합니다 (시스템에 의해 죽었습니까?) . 다른 한편으로, (메모리 누수로 인해) 추락하거나 죽지 않는 시간이 지나면 (메모리 누수로 인해) 100MB 이상 을 사용하여 휴대 전화에서 실행되는 앱 이 있습니다. 따라서 얼마나 많은 RAM을 절약 할 수 있는지 결정할 때 앱 자체에 달려 있습니다. 이것이 어떻게 가능한지? (저는 768MB RAM의 HTC One S로 테스트를 수행했습니다)
면책 조항 : 나는 안드로이드 작업 관리자 응용 프로그램과 관련이 없습니다.
Android 애플리케이션 (시스템 앱이 아님)이 사용할 수있는 최대 메모리 양 (메가 바이트 / 총 RAM의 백분율)은 얼마입니까?
장치마다 다릅니다. getMemoryClass()
onActivityManager
은 코드가 실행되는 장치의 값을 제공합니다.
안드로이드 버전간에 차이가 있습니까?
예, 수년 동안 OS 요구 사항이 증가하고 장치가 일치하도록 조정되는 한.
장치 제조업체와 관련하여 차이가 있습니까?
예, 제조업체가 장치를 제조하는 한 크기는 장치마다 다릅니다.
앱이 사용할 수있는 RAM의 양을 결정할 때 고려해야 할 "부수 요인"은 무엇입니까?
나는 "부작용"이 무엇을 의미하는지 전혀 모른다.
초기 장치의 앱당 한도는 16MB입니다. 이후 장치는 24MB 또는 32MB로 증가
맞습니다. 해상도가 높을수록 비트 맵이 커지므로 화면 해상도는 결정적인 요소이므로 태블릿 및 고해상도 전화기의 값은 더 높은 경향이 있습니다. 예를 들어, 48MB 힙을 가진 장치가 표시되며 그보다 높은 값이 있으면 놀라지 않을 것입니다.
앱이 한도를 초과 할 수 있습니까?
해당 앱의 작성자가 자신이하는 일을 알고 있다고 가정합니다. 핵심 안드로이드 엔지니어가 앱의 메모리 사용이 어렵다는 것을 고려할 때 , 해당 앱이 반드시 정확한 결과를 제공한다고 가정하지는 않습니다.
즉, 네이티브 코드 (NDK)에는 힙 제한이 적용되지 않습니다. 또한 Android 3.0부터 앱은 일반적으로 수백 MB 범위의 "대형 힙"을 요청할 수 있지만 대부분의 앱에서 잘못된 형식으로 간주됩니다.
또한 약 30-40 메가 바이트를 사용할 때 일부 앱이 OutOfMemoryException으로 충돌하는 것으로 나타났습니다.
Android 가비지 수집기는 압축 가비지 수집기가 아닙니다. 예외는 실제로이어야 CouldNotFindSufficientlyLargeBlockOfMemoryException
하지만 아마도 너무 장황한 것으로 간주되었습니다. 요청한 블록을 할당OutOfMemoryException
할 수없고 힙을 완전히 소모하지 않았 음을 의미합니다.
2018 년 말이 되서 상황이 바뀌 었습니다.
우선 : 앱을 실행하고 Android Studio에서 Android 프로파일 러 탭을 엽니 다. 당신은 얼마나 많은 메모리를 소비하는지 볼 것입니다, 당신은 놀랄 것이지만 많은 RAM을 할당 할 수 있습니다.
또한 여기에는 메모리 관리에 대한 심층적 인 모양을 제공 할 수있는 메모리 프로파일 러 사용 방법에 대한 자세한 지침이 포함 된 공식 문서의 유용한 기사 가 있습니다.
그러나 대부분의 경우 일반 Android 프로파일 러로 충분합니다.
일반적으로 앱은 50Mb의 RAM 할당으로 시작하지만 일부 사진을 메모리에로드하기 시작하면 즉시 90Mb까지 점프합니다. 미리로드 된 사진 (각각 3,5Mb)이있는 ViewPager로 활동을 열면 몇 초 만에 190Mb를 쉽게 얻을 수 있습니다.
그러나 이것이 메모리 관리에 문제가 있다는 것을 의미하지는 않습니다.
The best advice I can give is to follow the guidelines and best practices, use top libraries for image loading (Glide, Picasso) and you'll be OK.
But if you need to tailor something and you really need to know how much memory you can allocate manually you can get total free memory and calculate a pre-determined portion (in %) out of it. In my case, I needed to cache decrypted photos in memory so I don't need to decrypt them everytime user slides through the list.
For this purpose you can use ready to use LruCache class. It's a cache class which automatically tracks of how much memory your objects allocate (or the number of instances) and removes the oldest to keep the recent by their usage history. Here is a great tutorial on how to use it.
In my case, I created 2 instances of caches: for thumbs and attachments. Made them static with singleton access so they are available globally throughout the app.
cache class:
public class BitmapLruCache extends LruCache<Uri, byte[]> {
private static final float CACHE_PART_FOR_THUMBS_PRC = 0.01f; // 1% (Nexus 5X - 5Mb)
private static final float CACHE_PART_FOR_ATTACHMENTS_PRC = 0.03f;// 3% (Nexus 5X - 16Mb)
private static BitmapLruCache thumbCacheInstance;
private static BitmapLruCache attachmentCacheInstance;
public static synchronized BitmapLruCache getDecryptedThumbCacheInstance() {
if (thumbCacheInstance == null) {
int cacheSize = getCacheSize(CACHE_PART_FOR_THUMBS_PRC);
//L.log("creating BitmapLruCache for Thumb with size: " + cacheSize + " bytes");
thumbCacheInstance = new BitmapLruCache(cacheSize);
return thumbCacheInstance;
} else {
return thumbCacheInstance;
}
}
public static synchronized BitmapLruCache getDecryptedAttachmentCacheInstance() {
if (attachmentCacheInstance == null) {
int cacheSize = getCacheSize(CACHE_PART_FOR_ATTACHMENTS_PRC);
// L.log("creating BitmapLruCache for Attachment with size: " + cacheSize + " bytes");
attachmentCacheInstance = new BitmapLruCache(cacheSize);
return attachmentCacheInstance;
} else {
return attachmentCacheInstance;
}
}
private BitmapLruCache(int maxSize) {
super(maxSize);
}
public void addBitmap(Uri uri, byte[] bitmapBytes) {
if (get(uri) == null && bitmapBytes != null)
put(uri, bitmapBytes);
}
public byte[] getBitmap(Uri uri) {
return get(uri);
}
@Override
protected int sizeOf(Uri uri, byte[] bitmapBytes) {
// The cache size will be measured in bytes rather than number of items.
return bitmapBytes.length;
}
}
This is how I calculate available free RAM and how much I can bite out of it:
private static int getCacheSize(float partOfTotalFreeMemoryToUseAsCache){
final long maxMemory = Runtime.getRuntime().maxMemory();
//Use ... of available memory for List Notes thumb cache
return (int) (maxMemory * partOfTotalFreeMemoryToUseAsCache);
}
And this is how I use it in Adapters to get cached image:
byte[] decryptedThumbnail = BitmapLruCache.getDecryptedThumbCacheInstance().getBitmap(thumbUri);
and how I set it into cache in background thread (regular AsyncTask):
BitmapLruCache.getDecryptedThumbCacheInstance().addBitmap(thumbUri, thumbBytes);
My app targets API 19+ so devices are not old and these portions of available RAM are good enough for cache in my case (1% and 3%).
Fun fact: Android does not have any APIs or other hacks to get the amount of memory allocated to your app, it's calculated on the fly based on various factors.
추신 : 나는 정적 클래스 필드를 사용하여 캐시를 보유하고 있지만 최신 Android 지침에 따라 해당 목적으로 ViewModel 아키텍처 구성 요소 를 사용하는 것이 좋습니다 .
앱당 메모리 제한은 화면 크기 및 Android 버전에 따라 다릅니다. https://drive.google.com/file/d/0B7Vx1OvzrLa3Y0R0X1BZbUpicGc/view?usp=sharing
출처 : Android 호환성 다운로드 http://source.android.com/compatibility/downloads.html ; 호환성 정의 문서 (CDD), 섹션 가상 머신 호환성 또는 런타임 호환성
참고 URL : https://stackoverflow.com/questions/18675557/what-is-the-maximum-amount-of-ram-an-app-can-use
'Programming' 카테고리의 다른 글
.net 4에서 async-await 사용 (0) | 2020.06.23 |
---|---|
기본 인코딩이 ASCII 일 때 파이썬이 유니 코드 문자를 인쇄하는 이유는 무엇입니까? (0) | 2020.06.23 |
데이터베이스 클러스터 및로드 밸런싱 (0) | 2020.06.23 |
왜 주입 된 클래스 이름이 있습니까? (0) | 2020.06.23 |
NerdDinner의 ASP.NET MVC에서 ModelState.IsValid가 유효한 것은 무엇입니까? (0) | 2020.06.23 |