Asked  7 Months ago    Answers:  5   Viewed   56 times

I am writing an Android application which uses several 3D models. Such a model with textures can take up a lot of memory. I found out the manufacturer sets a limit on the heap size an application can use. For example my tablet Samsung Galaxy Tab 8.9 P7310 can take up 64MB of memory.

Is there a way to increase this size of memory an application can use?

 Answers

33

You can use android:largeHeap="true" to request a larger heap size, but this will not work on any pre Honeycomb devices. On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above.

The only way to have as large a limit as possible is to do memory intensive tasks via the NDK, as the NDK does not impose memory limits like the SDK.

Alternatively, you could only load the part of the model that is currently in view, and load the rest as you need it, while removing the unused parts from memory. However, this may not be possible, depending on your app.

Tuesday, June 1, 2021
 
Vlad
answered 7 Months ago
80

Instead of increasing heap size you can do some thing better. As you said that you are maintaining cache in you application which is implemented using SoftReferences. The best thing is to use LruCache you can do some thing like this:

private LruCache<String, Bitmap> bitmapCache;
final int memClass;
int cacheSize;

memClass = ((ActivityManager) context.getSystemService(
    Context.ACTIVITY_SERVICE)).getMemoryClass();

Return the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

cacheSize = 1024 * 1024 * memClass / 10;
bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
  @Override
  protected int sizeOf(String key, Bitmap value) {
    return value.getHeight() * value.getRowBytes();
 }
};

it will remove the bitmap images from LruCache if the memory exceeds the located memory to LruCache and load the new image in it.

Thursday, July 29, 2021
 
BetaRide
answered 4 Months ago
30

You will need to use PackageManager to get the package information about whatever app you select in the list. I haven't done this with a Spinner but i'm sure it should work the same as it did in my ListView.

appspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int app_pos, long app_id) {
        // TODO Auto-generated method stub
        String app_selected=parent.getItemAtPosition(app_pos).toString();

        final PackageManager pm = getPackageManager();
        //get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(
                PackageManager.GET_META_DATA);
        int UID;
        //loop through the list of installed packages and see if the selected
        //app is in the list
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(app_selected)){
                //get the UID for the selected app
                UID = packageInfo.uid;
                break; //found a match, don't need to search anymore
            }

        }

        //Do whatever with the UID
        Log.i("Check UID", "UID is: " + UID);               

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        return;
    }
});

You might want to see how packageName returns the packageName so you can try to match it with whatever was selected.

hope this points you in the right direction and helps you out. Good Luck.

Friday, July 30, 2021
 
Taha
answered 4 Months ago
54

Set your compileSdkVersion 'android-P' and targetSdkVersion 28

EDIT: Now compileSdkVersion 28 should also work meaning you no longer have to set it to android-P

Saturday, September 18, 2021
 
oroshnivskyy
answered 3 Months ago
70

Purely by guessing, I changed the following in my gradle config

from:

  testOptions {
    execution 'ANDROID_TEST_ORCHESTRATOR'
  }

to

  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }

and all seems to work.

Sunday, September 19, 2021
 
Shevek
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share