gpt4 book ai didi

java - Activity 占用 140mb 内存

转载 作者:太空狗 更新时间:2023-10-29 15:40:10 27 4
gpt4 key购买 nike

我正在尝试创建一个社交媒体应用程序。但它需要大约 300mb 内存。所以我的主页上有 5 个包含帖子的 fragment 。总体内存使用量为 250-300mb

然后为了测试,我禁用了这些 fragment ,但在没有任何大操作的情况下家庭 Activity 仍然消耗 140mb。

所以,

这是我的类(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);

}


}

函数

    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在该类中没有添加 fragment ,但内存使用量为 120-140

Memory Usage

我敢肯定,我在所有其他 Activity 或 fragment 中重复同样的错误。一旦我知道了问题,我就可以为其他部分修复它。 Heap Dump

需要帮助:(

最佳答案

我觉得很正常。它实际上并没有吃掉 140MB 的 RAM。在新的 Android 监视器上,AS3 数字与以前的 AS2 有点不同。在user guide here , 它是这样写的:

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.

我曾经在我以前的应用程序上看到 200MB 以上的内容,其中有 4 个带有底部导航的 fragment (但在 Leakcanary 上没有发现任何内容)。

关于java - Activity 占用 140mb 内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353645/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com