gpt4 book ai didi

java - 重用单个对象而不是创建新对象

转载 作者:行者123 更新时间:2023-11-30 07:01:31 26 4
gpt4 key购买 nike

案例研究
我有一种方法可以扫描文件系统中的某些条目,并在找到时将它们存储为新对象。这些对象包括几个表示条目位置的字符串和表示条目状态的标志。

由于我实例化的每个新对象占用 > 16 个字节,如果找到 1000 个条目,我将使用 (16 * 1000) 个字节!由于我一次只需要一个实例,因此我想重用单个对象(即使用 new 一次)并更新其字段。

这就是我现在处理对象的方式:

for (final Object object : scanFor(PATTERN)) {
if (aName.equals(object.getName()) {
... use the object
}
}

我想我可以使用与 ResultSet 相同的技术。
有什么建议吗?

附注我知道 16kb 内存不算什么,但这只是一个例子。

编辑:我看到有人投票结束我的问题,因为不清楚。说实话我真的不知道还能写什么。

编辑,真实代码:
调用者:

DspPgmRef dspPgmRef = new DspPgmRef("MYLIBRARY", "*ALL", J400Type.ALL);
dspPgmRef.addFilter(DspPgmRefEntryField.REFERENCED_OBJECT, name);

for (final DspPgmRefEntry entry : dspPgmRef.run()) {
if (entry.referencedObject.equals(name)) {
users.add(J400ObjectFactory.get("*LIBL", entry.object));
}
}

DspPgmRef实现:

public final class DspPgmRef implements J400Command<List<DspPgmRefEntry>>
{
/**
* Rappresenta un valore di ritorno del comando
*/
public static class DspPgmRefEntry
{
public final String library;
public final String object;
public final String referencedLibrary;
public final String referencedObject;
public final String referencedType;

public DspPgmRefEntry(
final String library,
final String object,
final String referencedLibrary,
final String referencedObject,
final String referencedType) {
this.library = library;
this.object = object;
this.referencedLibrary = referencedLibrary;
this.referencedObject = referencedObject;
this.referencedType = referencedType;
}
}

/**
* Campi dei riferimenti ritornati dal comando
*/
public enum DspPgmRefEntryField
{
LIBRARY("WHLIB"),
OBJECT("WHPNAM"),
REFERENCED_LIBRARY("WHLNAM"),
REFERENCED_OBJECT("WHFNAM"),
REFERENCED_TYPE("WHOTYP");

/**
* Nome reale del campo di database
*/
public final String value;

private DspPgmRefEntryField(final String value) {
this.value = value;
}
}

private final String _library;
private final String _object;
private final J400Type _type;
private final Map<DspPgmRefEntryField, String> _filters;

{
_filters = new EnumMap<>(DspPgmRefEntryField.class);
}

public DspPgmRef(final String library, final String object, final J400Type type) {
_library = library;
_object = object;
_type = type;
}

public DspPgmRef(final J400GenericObject object) {
this(object.getLibrary(), object.getName(), object.getType());
}

@Override
public List<DspPgmRefEntry> run() {
final String tempLibrary = J400Util.getRandomAS400String();
final String tempFile = J400Util.getRandomAS400String();
final int filtersSize = _filters.size();

final StringBuilder builder = new StringBuilder(120 + filtersSize * 20);
builder.append("DSPPGMREF PGM(");
builder.append(_library);
builder.append("/");
builder.append(_object);
builder.append(") OUTPUT(*OUTFILE) OBJTYPE(");
builder.append(_type.ASType);
builder.append(") OUTFILE(");
builder.append(tempLibrary);
builder.append("/");
builder.append(tempFile);
builder.append(")");

final J400Connection connection = J400Connection.instance();
connection.createLibrary(tempLibrary, "Libreria temporanea per DSPDBR");

if (!connection.executeCommand(builder.toString()).containsKey("CPF3030")) {
return Collections.EMPTY_LIST;
}

builder.delete(0, builder.length());
builder.append("select distinct WHPNAM, WHLNAM, WHFNAM, WHOTYP from ");
builder.append(tempLibrary);
builder.append(".");
builder.append(tempFile);

if (filtersSize > 0) {
builder.append(" where ");

for (final Entry<DspPgmRefEntryField, String> filter : _filters.entrySet()) {
builder.append(filter.getKey().value);
builder.append(" = '");
builder.append(filter.getValue());
builder.append("' and ");
}

final int stringLenght = builder.length();
builder.delete(stringLenght - 5, stringLenght);
}

PreparedStatement statement = null;
ResultSet result = null;

try {
statement = connection.getConnection().prepareStatement(builder.toString());
result = statement.executeQuery();

final List<DspPgmRefEntry> references = new ArrayList<>(128);

while (result.next()) {
references.add(new DspPgmRefEntry(
_library,
result.getString(DspPgmRefEntryField.OBJECT.value).trim(),
result.getString(DspPgmRefEntryField.REFERENCED_LIBRARY.value).trim(),
result.getString(DspPgmRefEntryField.REFERENCED_OBJECT.value).trim(),
result.getString(DspPgmRefEntryField.REFERENCED_TYPE.value).trim()));
}

return Collections.unmodifiableList(references);
} catch (final SQLException e) {
e.printStackTrace();
} finally {
try {
if (result != null) {
result.close();
}

if (statement != null) {
statement.close();
}
} catch (final SQLException e) {
e.printStackTrace();
}

connection.deleteLibrary(tempLibrary);
}

return Collections.EMPTY_LIST;
}

public void addFilter(final DspPgmRefEntryField field, final String value) {
_filters.put(field, value);
}

public void removeFilter(final DspPgmRefEntryField field) {
_filters.remove(field);
}

public void clearFilters() {
_filters.clear();
}
}

最佳答案

假设您建议的是从 scanFor 返回相同对象实例的性能影响和代码异味。这段代码中的方法:

for (final Object object : scanFor(PATTERN)) {
if (aName.equals(object.getName()) {
... use the object
}
}

根据代码其余部分的详细信息(例如, equals 检查为真的频率以及 ... use the object 中发生的情况),这确实可能会大大节省分配率。然而,它确实违反了 Iterable<T>.iterator().next() 的普遍理解的行为。 - 它每次都会返回一个唯一的对象。这已在 some length before 中讨论过。 。如果您使用此技巧测量出对您来说很重要的性能提升,那么它可能仍然是值得的,但一如既往的测量。

Java Mission Control包含在 (Oracle) JDK 中,免费供非商业用途使用,可以测量实际对象分配率和位置。您可以使用它来验证临时对象是否占整体分配的重要部分(在某些情况下,内联+转义分析可能能够完全消除它们,从而使您假设的问题变得毫无意义)。

关于java - 重用单个对象而不是创建新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40851441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com