Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 占用140mb内存的活动_Java_Android_Memory Management_Memory Leaks - Fatal编程技术网

Java 占用140mb内存的活动

Java 占用140mb内存的活动,java,android,memory-management,memory-leaks,Java,Android,Memory Management,Memory Leaks,我正在尝试创建一个社交媒体应用程序。但它需要大约300mb的内存。所以我的主页上有5个包含帖子的片段。总的来说,内存使用量为250-300mb 然后为了测试,我禁用了这些片段,但在没有任何大型操作的情况下,home活动仍消耗了140mb 所以 这是我的班级 public class HomePage extends AppCompatActivity { private Drawer result = null; private Boolean isCoverEdit = fal

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

然后为了测试,我禁用了这些片段,但在没有任何大型操作的情况下,home活动仍消耗了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;

            }
        });
    }
全班: 在该类中,没有添加片段,但内存使用率为120-140

我敢肯定,我在所有其他活动或片段中重复同样的错误。一旦我知道问题所在,我就可以为其他部件修复它。


需要帮助:(

我认为这是正常的。它实际上并没有消耗140MB的内存。在新的安卓显示器上,AS3的编号与以前的AS2有所不同。在中,它写道:

与之前Android Monitor工具的内存计数相比, 新的内存分析器以不同的方式记录您的内存,因此它可能 似乎您的内存使用率现在更高了。内存档案器将监视 一些额外的类别会增加总数,但如果你只关心 关于Java堆内存,那么“Java”编号应该相似 使用上一个工具中的值

虽然Java数字可能与您的不完全匹配 在Android Monitor中看到,新的数字占所有物理量的百分比 自应用程序启动以来已分配给应用程序Java堆的内存页 是从合子分叉的。因此这提供了一个精确的 您的应用程序实际使用的物理内存量

目前,内存探查器还显示了一些误报 应用程序中实际属于分析工具的内存使用情况。 为~100k对象添加了高达10MB的内存。在未来的版本中 在这些工具中,这些数字将从您的数据中筛选出来

我以前的应用程序有4个底部导航的片段(在Leakcanary上找不到任何内容),我以前看到的是200MB+。

也许这会有所帮助

检查你的

  • 封面图片
  • 侧面图
  • 闪屏图像
检查图像的
大小
,它们会对内存造成严重影响

在您使用的代码中

UCrop.of(absolutePath, Uri.parse(imageURI))
                        .withAspectRatio(16, 9)
                        .withMaxResultSize(1280, 720)
                        .start(HomePage.this);
如果最终在ImageView中以128x96像素的缩略图显示,那么将1024x768像素的图像加载到内存中是不值得的

这意味着你必须是具体的,或者如果你需要这种质量,使用它,但确保这将花费大量的内存

看看这里

如果你在项目中使用的图像作为初始屏幕,你可以使用工具优化图像,在质量和文件大小上达到完美平衡

工具-检查此项:


我建议您检查资源。可绘制文件夹可能包含图片,还可以检查音乐。检查初始图像大小,如果有音乐,请检查其大小以及视频。

进行堆转储,看看哪些类使用了您所有的内存。很可能是图像。但我们无法知道,你必须在这里做些准备工作。我刚刚添加了堆转储屏幕截图。它在顶部显示FinalizerReference和com.google.android.gms。但是我怎么知道是什么实际对象或进程导致了这个问题。。谢谢你的回答。@GabeSchenit是关于RAM内存,而不是存储。虽然大的可绘制性确实在增加,但是内存使用情况,你的回答是不正确的,因为你说的是应用程序使用的存储空间。不只是存储空间。我还说的是RAM内存。在运行时,你的设备应该可以处理你的大型应用程序。但许多设备无法处理此类应用程序。因此它将失败。让我给你一些建议,将图片更改为低分辨率图像,并尝试它。希望它将工作