首页 \ 问答 \ 如何正确使用HashSet Android(How to use HashSet properly Android)

如何正确使用HashSet Android(How to use HashSet properly Android)

我在我的应用程序中使用hashset,我试图看看如何避免泄漏内存,我使用如下所示在onActivityResult中填写它,然后当用户单击按钮时我只需处理其中的内容然后完成活动。

据我所知,内存泄漏在这里是不可能的,因为hashSet不是静态的,所以一旦Activity完成,没有任何内容会指向HashSet,当GC启动时它将是免费的,这个假设是否正确?

public class TestActivity extends Activity {


    HashSet<String> hashSet = new HashSet<String>();


        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 //process what's in the hashset 
                 //do I need to do something like hashSet.clear() 
                 //or something else like setting the hashSet to null?
                //then exit
                finish();
            }
        });


    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == SOME_RETURN_CODE) {

            hashSet.add(some string);
        }   
    }
}

I use hashset in my app and I'm trying to see how can I avoid leaking memory,I use as shown below by filling it out inside onActivityResult, then when the user clicks the button I simply process what's in there then finish the activity.

As far as I understood a memory leak isn't possible here because hashSet isn't static so as soon as the Activity finishes nothing will be pointing to the HashSet and it will be free when the GC kicks in, is this assumption correct?

public class TestActivity extends Activity {


    HashSet<String> hashSet = new HashSet<String>();


        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 //process what's in the hashset 
                 //do I need to do something like hashSet.clear() 
                 //or something else like setting the hashSet to null?
                //then exit
                finish();
            }
        });


    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == SOME_RETURN_CODE) {

            hashSet.add(some string);
        }   
    }
}

原文:https://stackoverflow.com/questions/29786384
更新时间:2023-09-08 16:09

最满意答案

你做得对,但逻辑运营商需要 更低

尝试将所有操作员切换为小写字母。


You are doing it right but the logic operators need to be lower cased.

Try switching all your operators to lower case.

相关问答

更多
  • 正如安德鲁在评论中提到的那样,问题在于/etc/apt/sources.list定义中的额外“ - ”: ... /etc/apt/sources.list: - file.managed: <<-- remove this dash; should be just 'file.managed' - template: jinja ... As Andrew mentioned in his comment, the problem is with the extra '-' in the / ...
  • 为什么不在测试中简单地将模板渲染为字符串,然后检查渲染模板是否正确? 有点类似于此: import jinja2 # assume it is an unittest function context = { # your variables to pass to template 'test_var': 'test_value' } path = 'path/to/template/dir' filename 'tempalte_to_test.tpl' rendered = jinja2. ...
  • 从概念上讲,您只需编写一个自定义模块并将其放在主服务器上的/ srv / salt / _modules目录中。 然后从命令行调用模块并将过滤器信息作为参数传递给它。 这是一个请求/示例 ,可以帮助您找到所需的数据。 Conceptually, you just write a custom module and put it in your /srv/salt/_modules directory on the master. Then you call the module from the comma ...
  • 不,那是无效的。 首先,每个都必须用单引号,其次,你必须明确地与每个字符进行比较 - 即if(code == 'S' || code == 'M' ...) 。 如果您正在检查这么多条件,那么是时候获取一个数组并使用循环了。 No, that is not valid. Firstly, each one would have to be in single quotes, and secondly, you must explicitly compare with each character- that ...
  • 首先,你不能使用{{ group }}语法,因为你已经在jinja表达式中。 所以你必须使用这个语法: 'server_access:' + group 其次,由于您的支柱数据包含服务器列表,因此我会提供一个空白列表[]作为默认值,以便表达式在支柱中有列表时也以相同的方式工作。 {% for servers in salt['pillar.get']('server_access:' + group, []) %} Jinja通常不会因为虚假陈述而被打破。 但是,如果该变量不是您所期望的类型,则很容易中断 ...
  • 你做得对,但逻辑运营商需要 更低 。 尝试将所有操作员切换为小写字母。 You are doing it right but the logic operators need to be lower cased. Try switching all your operators to lower case.
  • 这应该适用于大多数情况: state_c: module.run: - name: my.module - m_name: name_c - require: - module: state_a - module: state_b 另外,关于Salt使用单值dicts列表而不是平面词典:我认为这部分是因为像这样的情况你需要多个相同的密钥,部分是因为在订单重要时模拟有序的指令。 虽然我不是盐开发商。 This should work in most cas ...
  • Salt使用zeromq pub / sub接口与minions进行通信。 实际上,您只需要在主控防火墙上打开端口4505和4506。 小兵监听主人的一个端口,即“pub”端口,然后将结果返回给另一个端口上的主人。 主人从来没有真正将命令“推”给仆从。 小兵听取发布在pub端口上的命令。 这就是为什么你不需要在你的爪牙上打开任何传入的端口。 Salt uses a zeromq pub/sub interface to communicate with the minions. Indeed, you on ...
  • 看起来你正在使用Flask。 如果您将字典添加到应用程序的配置中,则可以在模板中调用该字典(Jinja将变量config识别为应用程序的配置),而无需将其传入。 #app.py app.config['navigation'] = {"Search": ["Google","Yahoo","Bing"]} def my_view(): return render_template('my_view.html') #layout.html {% for title, name in config.n ...
  • 我找到了这个问题的答案。 实际上有一个SaltStack的错误,它不考虑Windows注册表的32位/ 64位方面,只是调用常规的注册表。 Windows实际上会调用注册表重定向器来决定将密钥添加到wow6432node或常规节点的天气。 我实际上已经向SaltStack的github发布了一个bug。 见这里: https : //github.com/saltstack/salt/issues/13513#issuecomment-46373007 话虽这么说,我找到了解决这个问题的方法。 如果您使用S ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。