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

时间:2016-11-28 18:53:55

标签: java arrays object optimization memory-management

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

当每个新对象I instanciate占据> 16字节,如果找到1000个条目,我使用(16 * 1000)字节!由于我一次只需要一个实例,我想重复使用一个对象(即一次使用new)并更新其字段。

我现在如何处理对象:

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

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

P.S。 我知道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();
   }
}

1 个答案:

答案 0 :(得分:1)

让我们假设您所建议的是从此代码段中的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中,可以免费用于非商业用途,可以测量实际对象分配率和位置。您可以使用它来验证临时对象是否是整体分配的重要部分(在某些情况下,内联+转义分析可能能够完全消除它们,使您认为的问题无法解决)。