0

下面的代码出现堆空间错误。

任何人都知道如何优化此代码

这发生在大文件 [180MB] 上。方法参数有大约 50 个元标记键值对应于每个语言环境。处理 4500 页后出现错误。

注意:我尝试将foreach更改 iterator以使用iterator.remove()来释放空间。

public static String myChildPropsToString(final UnicodeProperties myLayoutProps) {       
    final StringBuilder sb = new StringBuilder(myLayoutProps.size());

    final String[] matchTarget = new String[] { StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE, StringPool.RETURN };
    final String[] replaceTargetBy = new String[] { "_SAFE_NEWLINE_CHARACTER_", "_SAFE_NEWLINE_CHARACTER_",
            "_SAFE_NEWLINE_CHARACTER_" };

     //COMMENTED TO TRY OUT ITERATOR.REMOVE
    //
    // for (final Map.Entry<String, String> entry : myLayoutProps.entrySet()) {
    // final String value = entry.getValue();
    //
    // if (Validator.isNotNull(value)) {
    // StringUtil.replace(value, matchTarget, replaceTargetBy);
    //
    // sb.append(entry.getKey());
    // sb.append(StringPool.EQUAL);
    // sb.append(value);
    // sb.append(StringPool.NEW_LINE);
    // }
    // }

    final Iterator<Entry<String, String>> propsIterator = myLayoutProps.entrySet().iterator();
    while (propsIterator.hasNext()) {
        final Entry<String, String> entry = propsIterator.next();

        if (Validator.isNotNull(entry.getValue())) {
            StringUtil.replace(entry.getValue(), matchTarget, replaceTargetBy);

            sb.append(entry.getKey());
            sb.append(StringPool.EQUAL);
            sb.append(entry.getValue());
            sb.append(StringPool.NEW_LINE);
        }
    }
    propsIterator.remove();

    return sb.toString();
}

从我的代码中,我将其设置为父属性 obj,如下所示:

UnicodeProperties myParentProps = new UnicodeProperties();
//Set some values to parent
UnicodeProperties myLayoutProps = new UnicodeProperties();
//Set some values to child
....
myParentProps.setProperty("childProp",myChildPropsToString(myLayoutProps));

任何帮助将不胜感激!

4

1 回答 1

0

尝试把 propsIterator.remove(); 一会儿里面。

final Iterator<Entry<String, String>> propsIterator = myLayoutProps.entrySet().iterator();
    while (propsIterator.hasNext()) {
        final Entry<String, String> entry = propsIterator.next();

        if (Validator.isNotNull(entry.getValue())) {
            StringUtil.replace(entry.getValue(), matchTarget, replaceTargetBy);

            sb.append(entry.getKey());
            sb.append(StringPool.EQUAL);
            sb.append(entry.getValue());
            sb.append(StringPool.NEW_LINE);
        }
        propsIterator.remove();
    }
于 2012-11-16T17:42:30.207 回答