草庐IT

Android Maps V2 内存泄漏 LocationClientHelper

coder 2023-12-09 原文

我们正在尝试追踪我们 Android 应用程序中 GoogleMap 上发生的内存泄漏,该泄漏在大约 40-50 次设备旋转后以 OOM 结束。该 map 设置了大约 3500 个标记。

该应用的 minSDK 为 9,因此使用 V4 支持库中的 SupportMapFragment。

我们尝试了多种方法,包括:

  • 缓存 LatLng 的
  • 缓存相机更新
  • 从 map 上移除标记
  • 从 map 中移除监听器
  • 移除所有监听器、标记等,这样我们就只有一个普通 map
  • 更新 Google Play 服务库
  • 更新支持库

分析 MAT 中的内存转储表明我们积累了很多实例 com.google.android.gms.location.internal.LocationClientHelper$ListenerTransport 我们不知道它们来自哪里。

有人知道内存泄漏的原因吗?

以下代码已经删除了所有标记和监听器,但仍然存在泄漏。首先是基类:

public abstract class BaseMapFragment extends Fragment {

public static final int MENU_ITEM_ID_SEARCH= 102;
public static final int MENU_ITEM_ID_SHOW_LIST= 100;
public static final int ZOOM_LEVEL_DEFAULT= 14;

private static final String SAVED_INSTANCE_LATITUDE= "savedLatitude";
private static final String SAVED_INSTANCE_LONGITUDE= "savedLongitutde";
private static final String SAVED_INSTANCE_ZOOM= "savedZoom";

protected static final String CLASSTAG= BaseMapFragment.class.getSimpleName();

private GoogleMap mMap;
private CameraUpdate mResumeCameraUpdate= null;
private double mSavedLatitude;
private double mSavedLongitude;
private float mSavedZoom;
private static View mView;

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mMap != null) {
        outState.putDouble(SAVED_INSTANCE_LATITUDE, mMap.getCameraPosition().target.latitude);
        outState.putDouble(SAVED_INSTANCE_LONGITUDE, mMap.getCameraPosition().target.longitude);
        outState.putFloat(SAVED_INSTANCE_ZOOM, mMap.getCameraPosition().zoom);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    if (savedInstanceState != null) {
        mSavedLatitude= savedInstanceState.getDouble(SAVED_INSTANCE_LATITUDE, Constants.EXTRA_VALUE_NONE);
        mSavedLongitude= savedInstanceState.getDouble(SAVED_INSTANCE_LONGITUDE, Constants.EXTRA_VALUE_NONE);
        mSavedZoom= savedInstanceState.getFloat(SAVED_INSTANCE_ZOOM, Constants.EXTRA_VALUE_NONE);
    }

    if (mView != null) {
        ViewGroup parent= (ViewGroup) mView.getParent();
        if (parent != null)
            parent.removeView(mView);
    }
    try {
        mView= inflater.inflate(R.layout.map_layout, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return mView;
}

protected GoogleMap initializeMap() {
    if (mMap != null) {
        if (mSavedLatitude != Constants.EXTRA_VALUE_NONE && mSavedLatitude != 0.0) {
            mResumeCameraUpdate= Context.getCamUpdate(mSavedZoom, mSavedLatitude, mSavedLongitude);
        } else {
            mResumeCameraUpdate= Context.getCamUpdate(mMap.getCameraPosition().zoom, mMap.getCameraPosition().target.latitude, mMap.getCameraPosition().target.longitude);
        }
    }

    SupportMapFragment mapFragment= (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    if (mapFragment == null) {
        mapFragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        if (mapFragment == null) {
            MapsInitializer.initialize(getActivity());
            mapFragment= SupportMapFragment.newInstance();
            mMap= mapFragment.getMap();
        } else {
            mMap= mapFragment.getMap();
        }
    } else {
        mMap= mapFragment.getMap();
    }

    // check if map is created successfully or not
    if (mMap == null) {
        Toast.makeText(getActivity().getApplicationContext(), R.string.map_create_unable, Toast.LENGTH_SHORT).show();
    } else {
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                if (mMap.getMyLocation() != null) {
                    CameraUpdate newLatLngZoom= Context.getCamUpdate(ZOOM_LEVEL_DEFAULT, mMap.getMyLocation());
                    mMap.animateCamera(newLatLngZoom);
                } else {
                    Toast.makeText(getActivity().getApplicationContext(), R.string.map_location_services_disabled, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

    }
    return mMap;
}

}

子类

public class MySupportMapFragment extends BaseMapFragment {

private LinearLayout mStaoButtonsLayout;
private ToggleButton mStaoButton;
private ToggleButton mGasStaoButton;

private Boolean mInitialLocationChange;
private CameraUpdate mResumeCameraUpdate;
private GoogleMap mMap;
private double mBundleLatitude;
private double mBundleLongitude;


@Override
public void addRequiredModelClasses(LinkedHashSet<Class<? extends ComergeModel<?>>> set) {
    set.add(AboModel.class);
    set.add(StationModel.class);
    super.addRequiredModelClasses(set);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putDouble(BUNDLE_EXTRA_CENTER_LATITUDE, mBundleLatitude);
    outState.putDouble(BUNDLE_EXTRA_CENTER_LONGITUDE, mBundleLongitude);        
}


@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(showSearchButton());

    final StationModel stationModel= getContext().getModel(StationModel.class);

    mStaoButtonsLayout= (LinearLayout) getActivity().findViewById(R.id.mapStaoButtons);
    mStaoButtonsLayout.setVisibility(View.VISIBLE);
    mStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.staoButton);
    mStaoButton.setChecked(stationModel.isStationButtonChecked());
    mGasStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.gasStaoButton);
    mGasStaoButton.setChecked(stationModel.isGasStationButtonChecked());

    mMap= initializeMap();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    addSearchButton(menu);
}

}

最佳答案

我之前遇到过类似的问题。我添加了以下代码来解决我的问题:

@Override 
public void onDestroy() {
     if (mMap != null) {
         mMap.setMyLocationEnabled(false);
     } 
 }

LocationClientHelper$ListenerTransport 似乎与 setMyLocationEnabled() 有关。我不得不注销一些回调以防止内存泄漏。

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

有关Android Maps V2 内存泄漏 LocationClientHelper的更多相关文章

  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

随机推荐