gpt4 book ai didi

java - 内存不足错误: Java heap space when trying to create ArrayList

转载 作者:行者123 更新时间:2023-12-01 18:09:48 32 4
gpt4 key购买 nike

我正在尝试编写一个程序,将 G 代码的所有 G1 行转换为 MOVX(G1 命令的 x 坐标)

例如。 G1 X0.1851 应变为 MOVX(0.1851)

目前,程序只是附加已读取的文本文件,并在同一文本文件中的旧代码下方打印新代码。

问题是,当我尝试创建 G-CodeX 之后的数字的数组列表时,我遇到了内存中的问题堆空间溢出。

我在G-Code行的每次迭代之后添加了一个clear()语句,以尝试防止数组列表变得越来越大,但它仍然保持溢出来了。

这是我的代码:

package textfiles;
import java.io.IOException;
import java.util.ArrayList;


public class FileData {

public static void main(String[] args) throws IOException {

String file_name = "C:/blabla";

try {
ReadFile file = new ReadFile(file_name);
WriteFile data = new WriteFile(file_name, true);
String[] aryLines = file.OpenFile();

int i;
int j;
int y;

for (i=0; i < aryLines.length; i++ ) { //goes through whole text file
System.out.println( aryLines[ i ]);

if (i == 0) {
data.writeToFile("");
System.lineSeparator();
}

char[] ch = aryLines[ i ].toCharArray();
ArrayList<Character> num = new ArrayList<Character>();
String xCo = null;


boolean counterX = false;

if ((ch[0]) == 'G' && ch[1] == '1') {

for (j = 0; j < ch.length; j++) { //goes through each line of text file

for (y = 0; counterX == true; y++) {
num.add(ch[j]);
}

if (ch[j] == 'X') {
counterX = true;
}

else if (ch[j] == ' ') {
counterX = false;
}
}
xCo = num.toString();
data.writeToFile("MOVX (" + xCo + ")");
}
num.clear();
}
}
catch (IOException e) {
System.out.println( e.getMessage() );
}

System.out.println("Text File Written To");

}
}

最佳答案

我建议避免将数据读入内存并使用流式传输。那么转换行的函数可能如下所示:

    public void convertFile(String fileName, String tmpFileName) throws IOException {
try (FileWriter writer = new FileWriter(tmpFileName, true)){
Pattern pG1_X = Pattern.compile("^G1 X");
Files.newBufferedReader(Paths.get(fileName)).lines().forEach(line -> {
try {
double x = Double.parseDouble(pG1_X.split(line)[1]); // get coordinate
String newLine = String.format("MOVX(%f)\n",x); // attempt to replace coordinate format
writer.write(newLine);
} catch (Exception e) {
LOGGER.log(Level.WARNING, String.format("error wile converting line %s", line), e);
}
});
}
}

演示其工作原理的测试用例:

package com.github.vtitov.test;

import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import java.nio.file.StandardCopyOption;

@RunWith(Theories.class)
public class ReadWriteTest {
final static Logger LOGGER = Logger.getLogger(ReadWriteTest.class.getName());

public void convertFile(String fileName, String tmpFileName) throws IOException {
try (FileWriter writer = new FileWriter(tmpFileName, true)){
Pattern pG1_X = Pattern.compile("^G1 X");
Files.newBufferedReader(Paths.get(fileName)).lines().forEach(line -> {
try {
double x = Double.parseDouble(pG1_X.split(line)[1]); // get coordinate
String newLine = String.format("MOVX(%f)\n",x); // attempt to replace coordinate format
writer.write(newLine);
} catch (Exception e) {
LOGGER.log(Level.WARNING, String.format("error wile converting line %s", line), e);
}
});
}
}


@DataPoints static public Long[] fileSizes() {return new Long[]{100L,10_000L,1_000_000L}; }
@Theory
public void readWriteTest(Long fileSize) throws Exception {
TemporaryFolder folder = TemporaryFolder.builder().parentFolder(new File("target")).build();
folder.create();
File file = folder.newFile(UUID.randomUUID() + ".txt");
File tmpFile = folder.newFile(file.getName() + ".tmp");
createFile(fileSize, file);
String filePath = file.getPath();
LOGGER.info(String.format("created file %s of %d lines", filePath, fileSize));
String tmpFilePath = filePath + ".tmp";
convertFile(filePath, tmpFilePath);
LOGGER.info(String.format("file %s converted to %s", filePath, tmpFilePath));
//assert false;
Files.move(new File(tmpFilePath).toPath(), new File(filePath).toPath(),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
LOGGER.info(String.format("file %s moved to %s", tmpFilePath, filePath));
folder.delete();
}

private void createFile(long fileSize, File file) throws Exception {
try (FileWriter writer = new FileWriter(file,true)) {
Random rnd = new Random();
rnd.doubles(fileSize).forEach(l -> {
try { writer.write(String.format("G1 X%f\n", l)); } catch (IOException ignored) {}
});
}
}

}

关于java - 内存不足错误: Java heap space when trying to create ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60487456/

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