我正在尝试创建社交媒体应用程序。但它占用了大约300mb的内存。所以我的主页上有5个包含帖子的片段。总体而言,内存使用量为250-300mb
然后,为了进行测试,我禁用了这些片段,但仍然主动活动消耗140mb而没有任何大的操作。
所以,
这是我的班级
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);
}
}
功能
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;
}
});
}
全班:here 在该类中,不添加任何片段,但内存使用量为120-140
我确定,我在所有其他活动或片段中重复同样的错误。一旦我知道了问题,我可以将其修复为其他部分。
需要帮助:(
答案 0 :(得分:8)
我认为这很正常。它实际上并没有吃掉140MB的RAM。在AS3的新Android监视器上,数字与之前的AS2略有不同。在user guide here中写道:
与之前的Android Monitor工具的内存计数相比, 新的Memory Profiler会以不同的方式记录您的内存,因此可能 看起来你的记忆力使用现在更高了。 Memory Profiler监视器 一些额外的类别,增加总数,但如果你只关心 关于Java堆内存,那么“Java”号应该是类似的 来自上一个工具的值。
虽然Java编号可能与您完全不符 在Android Monitor中看到,新号码占所有物理数据 已经分配给应用程序Java堆的内存页面 是从Zygote分叉的。所以这提供了准确的表示 你的应用实际使用了多少物理内存。
目前, Memory Profiler还显示了一些假阳性原生 应用中实际属于性能分析工具的内存使用情况。 为~100k对象添加了高达10MB的内存。在未来的版本中 对于这些工具,这些数字将从您的数据中过滤掉。
我曾经在我之前的应用程序上看到200MB +,其中有4个片段带有底部导航(在Leakcanary上没有找到任何内容)。
答案 1 :(得分:2)
也许这可以帮助..
检查你的
检查可能会影响内存的图像的size
!
UCrop.of(absolutePath, Uri.parse(imageURI))
.withAspectRatio(16, 9)
.withMaxResultSize(1280, 720)
.start(HomePage.this);
如果最终将在ImageView中以128x96像素的缩略图显示,则不值得将1024x768像素图像加载到内存中。
这意味着你必须是具体的,或者如果你需要这种质量,但要确保这会花费大量的内存。
Load a Scaled Down Version into Memory
如果您在项目中使用的图像作为启动画面,您可以使用工具优化图像,在质量和文件大小方面达到完美平衡。
答案 2 :(得分:0)
我建议你检查资源。可绘制文件夹可能包含图片,也可以检查音乐。检查启动图像大小,如果有音乐,请检查它的大小以及视频。