Friday, February 2, 2018

Activity taking 140mb memory

Leave a Comment

I'm trying to create a social media application. But it's taking about 300mb memory. So i had 5 fragments containing posts on my homepage. And overall the memory usage was 250-300mb

Then for testing, I disabled those fragments but still home activity is consuming 140mb without any big operations.

So,

here is my class

public class HomePage extends AppCompatActivity {     private Drawer result = null;     private Boolean isCoverEdit = false, isProfileEdit = false;     String username;     private RelativeLayout splash;     private Toolbar toolbar;     private StorageReference mStorage;     private ProgressDialog progressDialog;     private ImageView searchBtn;     private AHBottomNavigationViewPager fragContainer;     private AHBottomNavigation bottomNavigation;     private ImageView postBtn;     private View child;     private ImageView cover;     private CircleImageView profilePic;     private TextView star;     private TextView id;     private PopupMenu p;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_home_page);         if (FirebaseAuth.getInstance().getCurrentUser() == null) {             Intent i = new Intent(HomePage.this, Login.class);             startActivity(i);             finish();         } else {             getUsername();             initiateViews();             setupOnClickListeners();             setupBottomNavigation();              mStorage = FirebaseStorage.getInstance().getReference();             progressDialog = new ProgressDialog(this);          }       } 

Functions

    private void initiateViews() {         splash = findViewById(R.id.splash);         searchBtn = findViewById(R.id.search);         fragContainer = findViewById(R.id.frame);         toolbar = findViewById(R.id.toolbar);         bottomNavigation = findViewById(R.id.bottom_navigation);         postBtn = findViewById(R.id.post);         child = getLayoutInflater().inflate(R.layout.header, null);         profilePic = child.findViewById(R.id.pic);         id = child.findViewById(R.id.id);         cover = child.findViewById(R.id.cover);         star = child.findViewById(R.id.karma);     }   private void getUsername() {         username = new UserData(this).getUsername();          if (username==null){             FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {                 @Override                 public void onDataChange(DataSnapshot dataSnapshot) {                     username = dataSnapshot.getValue(String.class);                     SharedPreferences settings = getSharedPreferences("AyePref", MODE_PRIVATE);                     SharedPreferences.Editor editor = settings.edit();                     editor.putString("username", username);                     editor.apply();                     setupNavigationDrawer();                     FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());                  }                  @Override                 public void onCancelled(DatabaseError databaseError) {                  }             });         }else {             setupNavigationDrawer();             FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());         }     }      private void setupNavigationDrawer() {         result = new DrawerBuilder()                 .withActivity(HomePage.this)                 .withHeader(child)                 .withDisplayBelowStatusBar(false)                 .withTranslucentStatusBar(false)                 .withDrawerLayout(R.layout.material_drawer_fits_not)                 .addDrawerItems(                         new PrimaryDrawerItem().withName("Favourites").withIcon(GoogleMaterial.Icon.gmd_forum),                         new PrimaryDrawerItem().withName("Settings").withIcon(GoogleMaterial.Icon.gmd_settings),                         new PrimaryDrawerItem().withName("Contact").withIcon(GoogleMaterial.Icon.gmd_contact_mail),                         new PrimaryDrawerItem().withName("Commands").withIcon(GoogleMaterial.Icon.gmd_help),                         new PrimaryDrawerItem().withName("Log Out").withIcon(GoogleMaterial.Icon.gmd_security)                 )                  .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {                     @Override                     public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {                         if (drawerItem instanceof Nameable) {                             FragmentTransaction t = getSupportFragmentManager().beginTransaction();                             switch (position) {                                 case 3:                                     final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);                                     emailIntent.setType("plain/text");                                     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ayesupport@yandex.com"});                                     startActivity(Intent.createChooser(emailIntent, "Send mail..."));                                     break;                                 case 5:                                      break;                                 case 2:                                     Intent i= new Intent(HomePage.this, SettingsActivity.class);                                     startActivity(i);                                     break;                                 case 4:                                     Intent i2 = new Intent(HomePage.this,CommandsPage.class);                                     startActivity(i2);                                     break;                              }                           }                          return false;                     }                 }).build();          FirebaseDatabase.getInstance().getReference().child("userdata").child(username).addListenerForSingleValueEvent(new ValueEventListener() {             @Override             public void onDataChange(DataSnapshot dataSnapshot) {                 try {                     Glide.with(HomePage.this).load(dataSnapshot.child("pic").getValue(String.class)).override(100,100).thumbnail(0.5f).into(profilePic);                     //Glide.with(HomePage.this).load(dataSnapshot.child("cover").getValue(String.class)).into(cover);                  } catch (Exception e) {                     e.printStackTrace();                 }                 id.setText(username);                 star.setText("4.7");              }              @Override             public void onCancelled(DatabaseError databaseError) {              }         });     }      private void setupBottomNavigation() {         AHBottomNavigationItem item1 = new AHBottomNavigationItem("Topics", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_whatshot));         AHBottomNavigationItem item2 = new AHBottomNavigationItem("Rooms", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_group_work));         AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contacts", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_people));         AHBottomNavigationItem item4 = new AHBottomNavigationItem("Leaderboard", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_star));         AHBottomNavigationItem item5 = new AHBottomNavigationItem("Stats", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_insert_chart));         bottomNavigation.addItem(item1);         bottomNavigation.addItem(item2);         bottomNavigation.addItem(item3);         bottomNavigation.addItem(item4);         bottomNavigation.addItem(item5);         bottomNavigation.setInactiveColor(R.color.md_grey_800);         bottomNavigation.setAccentColor(R.color.md_grey_600);         bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_HIDE);         fragContainer.setOffscreenPageLimit(4);         HomePageAdapter adapter = new HomePageAdapter(getSupportFragmentManager());         setSupportActionBar(toolbar);         toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);         //fragContainer.setAdapter(adapter);         bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {             @Override             public boolean onTabSelected(int position, boolean wasSelected) {                 //fragContainer.setCurrentItem(position);                 return wasSelected;              }         });     } 

Full Class : here In that class no fragments are added but the memory usage is 120-140

Memory Usage

I'm sure, I'm repeating the same mistakes in all other activities or fragments. Once I know the problem I can fix it for other parts. Heap Dump

Need Help :(

3 Answers

Answers 1

I think it is normal. It is not eating 140MB of RAM actually. On new Android Monitor of AS3 numbers are a bit different than previous AS2. In the user guide here, it is written:

When compared to memory counts from the previous Android Monitor tool, the new Memory Profiler records your memory differently, so it might seem like your memory use is now higher. The Memory Profiler monitors some extra categories that increase the total, but if you only care about the Java heap memory, then the "Java" number should be similar to the value from the previous tool.

And although the Java number probably doesn't exactly match what you saw in Android Monitor, the new number accounts for all physical memory pages that have been allocated to your app's Java heap since it was forked from Zygote. So this provides an accurate representation of how much physical memory your app is actually using.

Currently, the Memory Profiler also shows some false-positive native memory usage in your app that actually belongs to the profiling tools. Up to 10MB of memory is added for ~100k objects. In a future version of the tools, these numbers will be filtered out of your data.

I used to see 200MB+ on my previous app that has 4 fragments with bottom navigation (and found nothing on Leakcanary).

Answers 2

maybe this can help ..

check your

  • cover image
  • profile picture
  • splash screen image

check the size of the images they can effect the memory so bad !

in your code you used

UCrop.of(absolutePath, Uri.parse(imageURI))                         .withAspectRatio(16, 9)                         .withMaxResultSize(1280, 720)                         .start(HomePage.this); 

it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.

thats mean you must be specific or if you need this quality use it but make sure this will cost a lot of memory.

take a look here

Load a Scaled Down Version into Memory

and there if the images that u use in the project as a splash screen you can use a tools Optimize your images with a perfect balance in quality and file size.

Tools - check this :

TinyPNG - Smart PNG and JPEG compression

Optimizilla

Answers 3

I would recommend you to check you resources. The drawable folder may contain pictures and also check for musics. Check the splash image size, if there are musics, check the size of it and also for videos.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment