【问题标题】:Should I set null to big items After building JSTL classic Select tag在构建 JSTL 经典 Select 标记后,我应该将 null 设置为大项目吗
【发布时间】:2014-02-21 17:35:56
【问题描述】:

我正在编写一个 JSTL 经典 Html 选择标签。该标签有一个用于构建标签的大项目(列表)。以下是我的代码:

public class MyBigSelectTag extends TagSupport {
private static final long serialVersionUID = 1L;

protected List<SelectItem> bigItems;

@Override
public void release() {

    // Release bigItems when the tag is releasing.
    this.bigItems = null;
}

@Override
public int doEndTag() throws JspException {
    JspWriter out = this.pageContext.getOut();
    try {
        out.write("<select>");
        for (SelectItem option : bigItems) {
            out.write("<option />");
        }
        out.write("</select>");
    } catch (IOException ex) {
        throw new JspTagException(ex.getMessage(), ex);
    } finally {
        // Do I have to set null to bigItems Here because it is too big?
        this.bigItems = null;
    }
    return super.doEndTag();
}

public void setBigItems(List<SelectItem> bigItems) {
    this.bigItems = bigItems;
}
}

我的问题是:我是否必须在 FINALLY 块中将 bigItems 属性设置为 NULL,因为 bigItems 太大了?请注意,我已经在 'release' 函数中将此属性设置为 null。

有人知道吗?非常感谢!

【问题讨论】:

    标签: java jsp jakarta-ee memory-leaks jstl


    【解决方案1】:

    不,您不必这样做。 release() 方法将在您的标签上调用,并将其设置为 null。

    即使您没有在 release() 方法中将其设置为 null,它也会在某个时候被垃圾收集(当标签被重用并且另一个列表替换当前的标签时)。

    另外,我怀疑你的名单太大了。记住一些数字很有用。为了可用,一个选择框不应该有超过几个项目,比如说 100,这对于选择框来说已经是一个很大的数字了。您的项目包含一个值和一个标签:假设它们都有 50 个字符,所以 100 个字符,所以 200 个字节。为引用、列表等添加一些位置,您应该得到 300 字节乘以 100 项,即 30000 字节,这意味着 30 KB。这远非大。它很小。

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2017-03-11
      • 2019-05-26
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 2010-11-19
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多