草庐IT

Android fragment 内存泄漏

coder 2023-12-10 原文

我在业余时间制作一个小应用程序,我使用了很多 fragment 。我对 fragment 还很陌生,但是我已经使用了很多资源来适本地使用……或者我是这么认为的。当我从一个 fragment 切换到另一个 fragment 时,随着我的堆不断增长,我遇到了内存泄漏。我之前使用 Activities 实现了我的应用程序,并且显着减少了内存泄漏。然而,现在我加入了 fragment 世界,我觉得在内存方面我好像是从头开始。

主 Activity .java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Setting the content view of the Activity
    setContentView(R.layout.main);

    // Setting up the actionbar.
    ActionBar actionbar = getActionBar();
    actionbar.setSubtitle("");
    actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
            | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    actionbar.setTitle("");
    actionbar = null;

    MainMenuFragment frag = new MainMenuFragment();

    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, frag).commit();
}

这个 MainActivity 还设置了 ActionBar。

主菜单 fragment .java

public class MainMenuFragment extends Fragment implements OnClickListener, LoaderCallbacks<Cursor> {

private static final String TAG = "MainMenuFragment";

SharedPreferences.Editor prefsEditor;
SharedPreferences prefs;

Bitmap[] picss = null;


static int width;

private static int FRIENDS = 0;
private static int PEOPLE = 1;
private static int MAP = 2;
private static int CAMERA = 3;
private static int TOP_TILE = 4;

LinearLayout topTile = null;
LinearLayout mainLayout;

private int NUM_OF_BUTTONS = 4;

ImageButton[] buttonList = null;

Bitmap background;

protected Activity activity;
protected String placeId = null;
protected ViewGroup container;
protected View view;

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState); 
    activity = getActivity();

    view = getView();

    setupObjects(view);
    setupLayoutsAndImages(view);

    // Populate the UI by initiating the loader to retrieve the 
    // details of the venue from the underlying Place Content Provider.
    if (placeId != null)
      getLoaderManager().initLoader(0, null, this);
  }



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main_screen , container, false);
    this.container = container;

    return view;
}

public void onDestroyView() {
    super.onDestroyView();

    Log.d(TAG, "onDestroyView()");

    buttonList[FRIENDS].setImageBitmap(null);
    buttonList[PEOPLE].setImageBitmap(null);
    buttonList[MAP].setImageBitmap(null);
    buttonList[CAMERA].setImageBitmap(null);
    topTile.setBackground(null);

    // Removes all the onClickListeners
    buttonList[FRIENDS].setOnClickListener(null);
    buttonList[PEOPLE].setOnClickListener(null);
    buttonList[MAP].setOnClickListener(null);
    buttonList[CAMERA].setOnClickListener(null);
    topTile.setOnClickListener(null);

    // Sets buttonList and the topTile to null
     buttonList = null;
     topTile = null;

    // Recycles the bitmaps and sets picss to null
    if (picss != null) {
        picss[FRIENDS].recycle();
        picss[PEOPLE].recycle();
        picss[MAP].recycle();
        picss[CAMERA].recycle();
        picss = null;
    }

    mainLayout.setBackground(null);
    background.recycle();

    unbindDrawables(view);
    view = null;
    activity = null;
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}


public void onClick(View v) {
    Log.d(TAG, "Got within here.");
    switch (v.getId()) {
    case R.id.b_friendButton:

        FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.fragment_container, new FriendsListFrag());
        trans.addToBackStack(null);
        trans.commit();
        break;
    case R.id.b_peopleButton:
        break;
    case R.id.b_mapButton:
        break;
    case R.id.b_CameraButton:
        break;
    case R.id.ll_Dynamic_Tile:
        break;
    }

}

private void setupObjects(View view) {

    Log.d(TAG, "Attempting to setupObjects.");

    mainLayout = (LinearLayout) view.findViewById(R.id.MainActivity_Main_Layout);
    topTile = (LinearLayout) view.findViewById(R.id.ll_Dynamic_Tile);

    buttonList = new ImageButton[NUM_OF_BUTTONS];
    buttonList[FRIENDS] = (ImageButton) view.findViewById(R.id.b_friendButton);
    buttonList[PEOPLE] = (ImageButton) view.findViewById(R.id.b_peopleButton);
    buttonList[MAP] = (ImageButton) view.findViewById(R.id.b_mapButton);
    buttonList[CAMERA] = (ImageButton) view.findViewById(R.id.b_CameraButton);

    picss = new Bitmap[5];

    buttonList[FRIENDS].setOnClickListener(this);
    buttonList[PEOPLE].setOnClickListener(this);
    buttonList[MAP].setOnClickListener(this);
    buttonList[CAMERA].setOnClickListener(this);

    int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
    int height = getActivity().getWindowManager().getDefaultDisplay().getHeight();

    background = GetImage.decodeSampledBitmapFromResource(getResources(),
            R.drawable.background2, width, height);

    System.out.println(mainLayout == null);

    mainLayout.setBackground(new BitmapDrawable(getResources(), background));

}

private void setupLayoutsAndImages(View view) {

    Log.d(TAG, "Attempting to setupLayoutsAndImages.");

    System.out.println("Width: " + width);
    final ImageButton myButton = (ImageButton) getView().findViewById(R.id.b_friendButton);
    getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            System.out.println("Fragwidth: " + getView().getWidth());
            System.out.println("FragHeight: " + getView().getHeight());
            System.out.println("Button: " + myButton.getWidth());
            width = myButton.getWidth();

            System.out.println("Width: " + width);

            getView().getViewTreeObserver().removeGlobalOnLayoutListener(this);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    width, width, 1.0f);
            int marg = (int) getResources().getDimension(
                    R.dimen.main_buttonpadding);
            params.setMargins(marg, marg, marg, marg);

            // Setting the params for the buttons.
            buttonList[FRIENDS].setLayoutParams(params);
            buttonList[PEOPLE].setLayoutParams(params);
            buttonList[MAP].setLayoutParams(params);
            buttonList[CAMERA].setLayoutParams(params);

            picss[FRIENDS] = GetImage.decodeSampledBitmapFromResource(
                    getResources(), R.drawable.tilefriendsmall, width, width);
            picss[PEOPLE] = GetImage.decodeSampledBitmapFromResource(
                    getResources(), R.drawable.tilepeoplesmall, width, width);
            picss[MAP] = GetImage.decodeSampledBitmapFromResource(
                    getResources(), R.drawable.tilemapsmall, width, width);
            picss[CAMERA] = GetImage.decodeSampledBitmapFromResource(
                    getResources(), R.drawable.tilecamerasmall, width, width);
            picss[TOP_TILE] = GetImage.decodeSampledBitmapFromResource(
                    getResources(), R.drawable.tilefriendlong, width, width);

            // Setting up the background for the buttons.
            buttonList[FRIENDS].setImageBitmap(picss[FRIENDS]);
            buttonList[FRIENDS].setAlpha(0.6f);
            buttonList[PEOPLE].setImageBitmap(picss[PEOPLE]);
            buttonList[PEOPLE].setAlpha(0.6f);
            buttonList[MAP].setImageBitmap(picss[MAP]);
            buttonList[MAP].setAlpha(0.6f);
            buttonList[CAMERA].setImageBitmap(picss[CAMERA]);
            buttonList[CAMERA].setAlpha(0.6f);

            LinearLayout.LayoutParams topParams = new LinearLayout.LayoutParams(
                    topTile.getWidth(), width);
            topParams.setMargins(marg, marg, marg, marg);
            topTile.setLayoutParams(topParams);
            topTile.setBackground(new BitmapDrawable(getResources(),
                    picss[TOP_TILE]));
            topTile.setAlpha(0.6f);
        }
    });

}

鉴于上面的代码,当我切换到 FriendsListFrag() 时,我保留了之前分配给 MainMenuFragment 的内存。我曾尝试使用内存分析器,但运气不佳。

最佳答案

您不断创建(并附加) fragment 的新实例。

改变这个:

 MainMenuFragment frag = new MainMenuFragment();

 getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, frag).commit();

类似于:

Fragment fragment = getSupportFragmentManager().
                   findFragmentById(R.id.fragment_container);
    if ( fragment == null ) {
        fragment = new MainMenuFragment();
            getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.fragment_container, fragment, "SOME_TAG_IF_YOU_WANT_TO_REFERENCE_YOUR_FRAGMENT_LATER")
            .commit();
    } 

FragmentManager 保留 fragment 的引用并自行处理。如果需要(在调用 onSaveInstance() 之前),您可以通过使用 FragmentManager 中的 .detach() 方法来删​​除它们。

你可以这样做:

Fragment f = getSupportFragmentManager().findFragmentByTag("SOME_TAG_IF_YOU_WANT_TO_REFERENCE_YOUR_FRAGMENT_LATER");

if ( f != null ) {
   getSupportFragmentManager().detach(f);
}  

注意:这都是伪代码,我没有测试,可能会有错别字,但你明白了

关于Android fragment 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023002/

有关Android fragment 内存泄漏的更多相关文章

  1. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  2. ruby-on-rails - Ruby 中的内存模型 - 2

    ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序

  3. 键删除后 ruby​​ 哈希内存泄漏 - 2

    你好,我无法成功如何在散列中删除key后释放内存。当我从哈希中删除键时,内存不会释放,也不会在手动调用GC.start后释放。当从Hash中删除键并且这些对象在某处泄漏时,这是预期的行为还是GC不释放内存?如何在Ruby中删除Hash中的键并在内存中取消分配它?例子:irb(main):001:0>`ps-orss=-p#{Process.pid}`.to_i=>4748irb(main):002:0>a={}=>{}irb(main):003:0>1000000.times{|i|a[i]="test#{i}"}=>1000000irb(main):004:0>`ps-orss=-p

  4. ruby-on-rails - HTTParty 的内存问题和下载大文件 - 2

    这会导致Ruby出现内存问题吗?我知道如果大小超过10KB,Open-URI会写入TempFile。但是HTTParty会在写入TempFile之前尝试将整个PDF保存到内存吗?src=Tempfile.new("file.pdf")src.binmodesrc.writeHTTParty.get("large_file.pdf").parsed_response 最佳答案 您可以使用Net::HTTP。参见thedocumentation(特别是标题为“流媒体响应机构”的部分)。这是文档中的示例:uri=URI('http://e

  5. ruby-on-rails - 内存中具有相同 ID 的更多对象? - 2

    在部署在heroku上的Rails应用程序(v:3.1)中,我在内存中获得了更多具有相同ID的对象。我的heroku控制台日志:>>Project.find_all_by_id(92).size=>2>>ActiveRecord::Base.connection.execute('select*fromprojectswhereid=92').to_a.size=>1这怎么可能?可能是什么问题? 最佳答案 解决方案根据您的SQL查询,您的数据库中显然没有重复条目。也许您的类项目中的size或length方法已被覆盖。我试过find_

  6. ruby - rails 3.0.7 内存泄漏 - 2

    我的两个不同的Rails应用程序的内存有一些奇怪的问题。这两个应用程序都使用rails3.0.7。每个Controller请求分配20-30-50MB的内存。在生产模式下,这个数量减少到5-10。但这是同样的事情。这是两个应用程序使用的gem列表:gem'pg'gem'haml'gem'sass'gem'devise'gem'simple_form'gem'state_machine'gem"globalize3","0.1.0.beta"gem"easy_globalize3_accessors"gem'paperclip'gem'andand'关闭所有这些gem不会给我任何结果。我

  7. ruby - 如何强制 Ruby 释放内存给操作系统 - 2

    正如标题,我有一个处理大量数据的ruby​​程序。该程序占用了所有内存,其中调用了系统命令hostname,并且发生错误无法分配内存-主机名我试过GC.start但它不起作用。那么如何强制ruby释放未使用的内存呢?OK,这是别人的测试代码,最后报错是big_var被回收了。但是内存仍然没有释放。require"weakref"defreportputs"#{param}:\t\tMemory"+`psax-opid,rss|grep-E"^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s+'KB'endbig_var=""#big

  8. ruby - 如何在 Ruby 中从内存中 HTTP 发布流数据? - 2

    我想上传我在运行时用Ruby生成的数据,就像从block中提供上传数据一样。我找到的所有示例仅展示了如何流式传输必须在请求之前位于磁盘上的文件,但我不想缓冲该文件。除了滚动我自己的套接字连接之外,最好的解决方案是什么?这是一个伪代码示例:post_stream('127.0.0.1','/stream/')do|body|generate_xmldo|segment|body 最佳答案 有效的代码。require'thread'require'net/http'require'base64'require'openssl'class

  9. ruby-on-rails - 如何仅修改内存中的 zip 文件? - 2

    我有一个Ruby应用程序,我需要修改现有的zip文件。我想在内存中构建zip文件并流回字节,而无需将文件写入文件系统。如果我最终在Heroku上托管它,我认为我无法写入文件系统。有谁知道这样做的方法吗?我看了Zip::ZipFile但看起来它总是想写入文件系统。我想“基于java实现”我将能够只获取压缩文件的字节,这可以在java中完成,但我看不到这样做的方法。编辑:我要问的与此基本相同,但针对Ruby而不是Python:Functiontocreatein-memoryzipfileandreturnashttpresponse 最佳答案

  10. ruby - 无法在 Ruby 中分配内存(无 MemoryError)? - 2

    我写了一个简单的脚本,它应该读取整个目录,然后通过去除HTML标签将HTML数据解析为普通脚本,然后将其写入一个文件。我有8GB内存和大量可用虚拟内存。当我这样做时,我有超过5GB的RAM可用。目录中最大的文件为3.8GB。脚本是file_count=1File.open("allscraped.txt",'w')do|out1|forfile_nameinDir["allParts/*.dat"]doputs"#{file_name}#:#{file_count}"file_count+=1File.open(file_name,"r")do|file|source=""tmp_sr

随机推荐