gpt4 book ai didi

java - 在 Java 中处理大字符串时出现 StringBuilder 内存不足错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:07:14 25 4
gpt4 key购买 nike

我从 String test += str; 开始,其中 test 以指数级增长,包含成千上万个字符。运行需要 45 分钟,可能是因为创建了大字符串并删除了垃圾。然后我像这样错开输入,将其带到 30 秒。

这似乎是一种廉价的方法,但效果很好:

  if (secondDump.length() > 50)
{
intermedDump = intermedDump + secondDump;
secondDump = "";
}

if (intermedDump.length() > 100)
{
thirdDump = thirdDump + intermedDump;
intermedDump = "";
}
if (thirdDump.length() > 500)
{
fourthDump = fourthDump + thirdDump;
thirdDump = "";
}
if (fourthDump.length() > 1000)
{
fifthDump = fifthDump + fourthDump;
fourthDump = "";
}
//with just this and not sixth. Runtime>>>> : 77343
if (fifthDump.length() > 5000)
{
sixthDump = sixthDump + fifthDump;
fifthDump = "";
}
//with just this. Runtime>>>> : 35903Runtime>>>> : 33780
if (sixthDump.length() > 10000)
{
fillerDump = fillerDump + sixthDump;
sixthDump = "";
}

然后我发现 StringBuilder 存在,从那以后我一直在尝试使用它,用它替换所有字符串操作。

问题是,我不断收到带有 Java 内存堆溢出的 java.lang.OutOfMemoryError。我认为该字符串太长而无法作为 StringBuilder 对象存储在内存中,因为它的进度大约是我之前的代码在因内存不足错误而崩溃之前所做的进度的 1/50。它只能处理不到一千个字符。

为什么一个字符串可以容纳整个输出而这不能接近?另外,如果我将文本附加到 JTextPane,需要多少内存?如果我将 StringBuilder 内容转储到 JTextpane 并继续附加和清除似乎也不起作用的 StringBuilder

这是现有的代码。页面只是一个被传递的对象:

protected void concatPlates(page PageA) throws IOException
{
if (backend.isFirstPage == false)
{
frontend.fillOutputPane("\n " +
" \n", PageA);
frontend.fillOutputPane(" " +
" \n", PageA);
frontend.fillOutputPane(" " +
" \n", PageA);
}
for (int i = 0; i < PLATELEN-1; i++)
{

if (arrLeftCol[i].length() == 0)
{
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
frontend.fillOutputPane(arrLeftCol[i].append(
arrRightCol[i]));
}
else
{
PageA.tempStrBuff = new StringBuilder(arrLeftCol[i].substring(0,40));
frontend.fillOutputPane(PageA.tempStrBuff.append(arrRightCol[i]));
}
arrLeftCol[i].append("");
arrRightCol[i].append("");

backend.isFirstPage = false;
}
}


//this is the frontend class
public static void fillOutputPane(String s, page PageA)
{
fillOutputPane(PageA.getStrBuf());
}
public static void fillOutputPane(StringBuilder stringBuild)
{
try
{
str.append(stringBuild);
}
catch (java.lang.OutOfMemoryError e)
{
System.out.println((str.length() * 16) /8);
//System.out.println(str);
System.out.println("out of memory error");
System.exit(0);
}
}

这里是错误:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at backend.fill(backend.java:603)
at frontend$openL.actionPerformed(frontend.java:191)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)

我认为这就是堆栈跟踪:

java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at frontend.fillOutputPane(frontend.java:385)
at page.concatPlates(page.java:105)
at backend.setPlate(backend.java:77)
at backend.fill(backend.java:257)
at frontend$openL.actionPerformed(frontend.java:191)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)81240560
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

显然您的应用程序没有足够的内存来完成操作。所以你需要为你的虚拟机指定内存标志。您可以尝试以下操作:

 java -Xms256m -Xmx512m YourApp

地点:

  • 您的程序在启动时分配的最小内存 Xms(在示例中为 256 MB)
  • Xmx 最大内存由您的程序分配,如果它需要更多(在示例中为 512 MB)

关于java - 在 Java 中处理大字符串时出现 StringBuilder 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8359693/

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