0

My app doesn't contain any native code but I am using the following libraries :

Fireabse auth,database,storage,messaging

Google Maps, Location

SendGrid - for sending emails

RazorPay

Glide

When my app starts, the RAM taken by Native section is around 180 MB and after I open a fragment which reads data from the real-time database a couple of times, the RAM usage by Native section shoots up to 800 MB!

How can I reduce this?

Edit:

My dependencies

implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.firebase:firebase-database:16.0.6'
    implementation 'com.google.firebase:firebase-storage:16.0.4'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.payumoney.sdkui:plug-n-play:1.5.0'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.razorpay:checkout:1.4.5'
    implementation files('libs/sendgrid-0.1.2-jar.jar')
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.github.mklimek:frame-video-view:1.3.3'

Here's how I query data from the database

DatabaseReference carReference = FirebaseDatabase.getInstance().getReference();
        carReference.child("carDetails").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ArrayList<String> carsOwnedByUser = new ArrayList<>();
                for (DataSnapshot carDetails : dataSnapshot.getChildren()) {
                    CarDetails car = carDetails.getValue(CarDetails.class);

                    if (userId.equals(car.getUserId())) {
                        carsOwnedByUser.add(car.getMake());
                    }
                }

                String selectedCar = sharedPreferences.getString(Constants.SHARED_PREFERENCES_SELECTED_CAR, "None");

                if (carsOwnedByUser.size() > 0) {
                    if (selectedCar.equals("None")) {
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString(Constants.SHARED_PREFERENCES_SELECTED_CAR, carsOwnedByUser.get(0));
                        editor.apply();
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

What I have tried:

I have tried disabling the disk Caching in Glide and I have tried setting the DatabaseReferenec.getInstance().setPersistacneEnabled() and keepSynced() to false but they didn't help.

Edit 2:

I checked the heap dump in the profiler and saw that Bitmap was taking 300 MB in the native section

Image

  • Are you sure you want us to solve your query without getting single line of your code? – DHAVAL A. Jul 04 '19 at 06:52
  • What piece of code can help in solving this problem? I am completely clueless about this. I guess I will add my dependencies and the way I am querying my realtime database. – arpit kumar Jul 04 '19 at 07:06
  • how much/which data are you reading from the DB? Are you sure that 800 mb ram is a problem? Check on lower-ram device (emulator) to see if it will cause problems. – Vladyslav Matviienko Jul 04 '19 at 07:14
  • Yup, on lower end devices(3 GB RAM or less) the app crashes with a FatalException. Could not allocate x bytes. – arpit kumar Jul 04 '19 at 07:29
  • This is probaly due to a `memory leak` where your app allocates memory that is never deallocated. Have you searched the internet for the term "android memory leak" (i.e. https://stackoverflow.com/questions/8174140/android-memory-leak ) ? – k3b Jul 04 '19 at 08:27
  • So please provide more information about your use of images: when and how do you load them, how many are on the screen at the same time, how big are they (width and height, original file size), do you process them (apply filters, crop etc.). – Codo Jul 05 '19 at 11:28
  • I use Glide to load them. Glide.with(context).load(getResources(...)).into(). I do no processing on the images and the images are 1-2 MB large. Nd Glide should take care about the size of image automatically. – arpit kumar Jul 05 '19 at 20:14

1 Answers1

0

First of all, below line is main reason for high memory usage.

carReference.child("carDetails"). addListenerForSingleValueEvent(...)

Above line load all carDetails's children from firebase database to device's memory. So avoid using that. If you still need to use that then paginate data using proper combination of startAt(), endAt(), limitToFirst() and limitToLast() query filters. Documentation

If you still unable to make correct pagination then go ahead and use proper database structure, so you can fetch required details using simple query. Best Practices for data structure

DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
  • This didn't help and in the profiler(heap dump) it shows Bitmap is taking around 300 MB in the Native section. https://imgur.com/lfiNueY – arpit kumar Jul 05 '19 at 11:01