我想限制JVM使用的最大内存.注意,这不仅仅是堆,我想限制此进程使用的总内存.
所以我在将一个文本文件读入我的程序时遇到了问题.这是代码:
try{
InputStream fis=new FileInputStream(targetsFile);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
//while(br.readLine()!=null){
for(int i=0;i<100;i++){
String[] words=br.readLine().split(" ");
int targetX=Integer.parseInt(words[0]);
int targetY=Integer.parseInt(words[1]);
int targetW=Integer.parseInt(words[2]);
int targetH=Integer.parseInt(words[3]);
int targetHits=Integer.parseInt(words[4]);
Target a=new Target(targetX, targetY, targetW, targetH, targetHits);
targets.add(a);
}
br.close();
}
catch(Exception e){
System.err.println("Error: Target File Cannot Be Read");
}
Run Code Online (Sandbox Code Playgroud)
我正在读取的文件是100行参数.如果我使用for循环它完美地工作.如果我使用while语句(for循环上面注释掉的那个),它会在50处停止.用户可能会运行带有任意行数的文件的程序,因此我当前的for循环实现赢了工作.
为什么线路while(br.readLine()!=null)停在50?我检查了文本文件,没有任何东西可以挂起来.
当我使用while循环时,我没有从try-catch中得到任何错误,所以我很难过.有人有主意吗?
我有3GB大小的数据文件要上传到GCP云存储中。我尝试了“ GCP上传对象”教程中的示例。但是,当我尝试上传时,出现以下错误。
java.lang.OutOfMemoryError: Required array size too large
Run Code Online (Sandbox Code Playgroud)
我尝试如下
BlobId blobId = BlobId.of(gcpBucketName, "ft/"+file.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.get().create(blobInfo, Files.readAllBytes(Paths.get(file.getAbsolutePath())));
return blob.exists();
Run Code Online (Sandbox Code Playgroud)
我该如何解决?是否可以使用GCP Cloud Storage Java客户端上传大文件?