【问题标题】:java.lang.OutOfMemoryError during String concatenation字符串连接期间的 java.lang.OutOfMemoryError
【发布时间】:2015-12-01 05:44:18
【问题描述】:

我在字符串连接期间得到 java.lang.OutOfMemoryError。有人可以帮我摆脱这个吗?下面是我的代码的外观。整个 if blkc 在循环下运行,并且当它处理“str”的字符串连接时,它会抛出 OutofemoryError。非常感谢您对此的任何帮助。

for (long j = mincollectiontime; j <= maxcollectiontime; j = j
        + timeintreval) {
    query = "select count(*) table1";

    ResultSet result2 = VerticaDBHandler.executequery(con2, query);
    System.out.println("Query:- " + query);

    String str = "";
    if (result2.isBeforeFirst()) {
        if (mysqlconn == null) {
            mysqlconn = DatabaseHandler.openDB();
        }

        while (result2.next()) {
            int isgap = Integer.parseInt(result2.getString(1));
            if (isgap == 0) {
                Date startime = EpochTimeHandler.epochToTimeStamp(j);
                Date endtime = EpochTimeHandler.epochToTimeStamp(j
                        + timeintreval);
                str = "NO DATA BETWEEN " + startime + " --- " + endtime
                        + " forInstanceId: " + instanceid
                        + " --InstanceName: " + instanceName + " in "
                        + perfTables[i];
                DatabaseHandler.LoadDB_dataGaps(mysqlconn,
                        perfTables[i], instanceid, instanceName,
                        VirtType, BelongstoDataCenter,
                        startime.toString(), endtime.toString(), str);
                System.out.println(str);
                str = "";
            } else {
                System.out.println("No Gap Seen");
            }
        }
    }
}

例外。

Exception thrown:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.LinkedHashMap.newNode(LinkedHashMap.java:256)
    at java.util.HashMap.putVal(HashMap.java:630)
    at java.util.HashMap.put(HashMap.java:611)
    at sun.util.resources.OpenListResourceBundle.loadLookup(OpenListResourceBundle.java:146)
    at sun.util.resources.OpenListResourceBundle.loadLookupTablesIfNecessary(OpenListResourceBundle.java:128)
    at sun.util.resources.OpenListResourceBundle.handleKeySet(OpenListResourceBundle.java:96)
    at java.util.ResourceBundle.containsKey(ResourceBundle.java:1807)
    at sun.util.locale.provider.LocaleResources.getTimeZoneNames(LocaleResources.java:262)
    at sun.util.locale.provider.TimeZoneNameProviderImpl.getDisplayNameArray(TimeZoneNameProviderImpl.java:122)
    at sun.util.locale.provider.TimeZoneNameProviderImpl.getDisplayName(TimeZoneNameProviderImpl.java:98)
    at sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter.getName(TimeZoneNameUtility.java:325)
    at sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter.getObject(TimeZoneNameUtility.java:281)
    at sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter.getObject(TimeZoneNameUtility.java:267)
    at sun.util.locale.provider.LocaleServiceProviderPool.getLocalizedObjectImpl(LocaleServiceProviderPool.java:281)
    at sun.util.locale.provider.LocaleServiceProviderPool.getLocalizedObject(LocaleServiceProviderPool.java:265)
    at sun.util.locale.provider.TimeZoneNameUtility.retrieveDisplayName(TimeZoneNameUtility.java:135)
    at java.util.TimeZone.getDisplayName(TimeZone.java:400)
    at java.util.Date.toString(Date.java:1045)
    at java.lang.String.valueOf(String.java:2982)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.test.perf.testpgm.main(testpgm.java:112)

【问题讨论】:

  • 需要查看更多代码。发布for循环?另外,您已经尝试过什么?你试过调试器吗?
  • 你能粘贴整个类吗
  • 完整的代码在上面内联编辑
  • 我不认为你的问题与字符串连接有关,我认为你只是加载了比你的记忆更多的数据。查看堆栈跟踪...
  • 您能否突出显示错误所在的代码行testpgm.java:112

标签: java out-of-memory string-concatenation


【解决方案1】:

似乎问题出在这里。

for (long j = mincollectiontime; j <= maxcollectiontime; j = j + timeintreval) 

您肯定排除了最大长度并在函数中进行调用。请发布更多代码,以便注意到具体问题。

【讨论】:

    【解决方案2】:

    可能这个问题与字符串连接无关,我认为这个问题与堆大小有关。一旦增加 IDE 的堆大小并运行程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 2011-04-16
      • 1970-01-01
      • 2019-06-11
      • 2013-03-19
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多