我正在寻找将文件输入流转换为大文件(文件大小为 100MB)并且它正在抛出和 java.lang.OutOfMemoryError : Java Heap space
import java.io.FileInputStream; import java.io.IOException;
import org.apache.commons.io.IOUtils;
public class TestClass {
public static void main(String args[]) throws IOException
{
//Open the input and out files for the streams
FileInputStream fileInputStream = new FileInputStream("file.pdf");
IOUtils.toByteArray(fileInputStream);
}
}
Run Code Online (Sandbox Code Playgroud)
实际的堆栈跟踪是
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.commons.io.output.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:322)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:463)
at TestClass.main(TestClass.java:12)
Run Code Online (Sandbox Code Playgroud)
我确实尝试使用以下方法处理它
public static byte[] toByteArray(InputStream is) {
if (is == null) {
throw new NullPointerException("The InputStream parameter is null.");
}
ByteArrayOutputStream baos = …Run Code Online (Sandbox Code Playgroud) 所以今天在学习Java时,我终于遇到了这个特殊错误.似乎这个错误非常普遍,并试图从中恢复已经获得了混合反应.有些人认为它在某些情况下是有用的,因此它是一个很好的"知道",有些人在他们的项目中使用它,而另一些则强烈反对 捕获这个错误的想法,然后很多其他人就像我一样困惑.
编辑:哦顺便说一句,这是我遇到的错误.我希望捕获这些错误并将增量值的值减少1/10,直到找到另一个错误
(依此类推......直到我找到上限)

由于我在Java中采取了我的步骤,并且在这个主题上找不到任何具体的内容,我想请求您提供有关此特定示例的帮助:
public class SpeedDemoClass {
static int iterations=0;
static int incrementor=10000000;
public static void main(String[] args) {
while(incrementor>1){
try{
iterations=iterations+incrementor;
iterator(iterations);
}
catch(Exception e){
System.out.println("So this is the limiting error: "+e);
iterations=iterations-incrementor;
incrementor=incrementor/10;
iterations=iterations+incrementor;
}
}
System.out.println("The upper limit of iterations is: "+iterations);
}
public static void iterator(int a){
long start_time= System.currentTimeMillis();
StringBuilder sbuild= new StringBuilder("JAVA");
for(int i=0;i<a;i++){
sbuild.append("JAVA");
}
System.out.println("Performing "+a+" append operations;"
+"process completed …Run Code Online (Sandbox Code Playgroud) 我完全按照本教程中的说明构建了 Spring Boot 应用程序来上传单个文件。我所做的更改如下所述。
我已添加以下属性application.properties:
spring.http.multipart.max-file-size=2048MB
spring.http.multipart.max-request-size=2048MB
Run Code Online (Sandbox Code Playgroud)
然后,我将以下 jvm 参数添加到项目运行配置中,以将 jvm 堆大小限制为 2GB,如本答案中所述:
-Xmx2048m
Run Code Online (Sandbox Code Playgroud)
接下来,我还确保我的 Spring Boot 应用程序针对 64 位 Java 运行,因为32 位 Java似乎需要“连续”堆空间来保留。
但当我尝试上传大文件时,它仍然给我同样的错误。
首先我尝试上传 20MB 的文件。有效。接下来我继续尝试价值 1GB 的 ubuntu iso,但它一直给我以下异常:
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236) ~[na:1.8.0_74]
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118) ~[na:1.8.0_74]
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) ~[na:1.8.0_74]
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) ~[na:1.8.0_74]
at org.springframework.util.StreamUtils.copy(StreamUtils.java:128) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:109) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.util.FileCopyUtils.copyToByteArray(FileCopyUtils.java:156) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getBytes(StandardMultipartHttpServletRequest.java:291) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at com.digitate.ignio.spring_boot_hdfs_file_upload.controller.UploadController.singleFileUpload(UploadController.java:73) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at …Run Code Online (Sandbox Code Playgroud) 我试图通过iText合并1000个PDF文件.我不确定内存泄漏发生在哪里.下面是示例代码.请注意,我将合并到父文件后立即删除child-pdf文件.请指出以下代码中的错误,或者有没有更好的方法来做内存概念.这个过程是通过servlet完成的(不是独立的程序)
FileInputStream local_fis = null;
BufferedInputStream local_bis = null;
File localFileObj = null;
for(int taIdx=0;taIdx<totalSize;taIdx++){
frObj = (Form3AReportObject)reportRows.get(taIdx);
localfilename = companyId + "_" + frObj.empNumber + ".pdf";
local_fis = new FileInputStream(localfilename);
local_bis = new BufferedInputStream(local_fis);
pdfReader = new PdfReader(local_bis);
cb = pdfWriter.getDirectContent();
document.newPage();
page = pdfWriter.getImportedPage(pdfReader, 1);
cb.addTemplate(page, 0, 0);
local_bis.close();
local_fis.close();
localFileObj = new File(localfilename);
localFileObj.delete();
}
document.close();
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Java-AWS API 在 AWS S3 上上传文件。问题是我的应用程序无法上传大型文件,因为堆已达到其限制。错误:java.lang.OutOfMemoryError:Java 堆空间
我个人认为扩展堆内存不是永久的解决方案,因为我必须将文件上传到 100 GB。我该怎么办 ?
这是代码片段:
BasicAWSCredentials awsCreds = new BasicAWSCredentials(AID, Akey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName("us-east-2"))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
InputStream Is=file.getInputStream();
boolean buckflag = s3Client.doesBucketExist(ABuck);
if(buckflag != true){
s3Client.createBucket(ABuck);
}
s3Client.putObject(new PutObjectRequest(ABuck, AFkey,file.getInputStream(),new ObjectMetadata() ).withCannedAcl(CannedAccessControlList.PublicRead));
Run Code Online (Sandbox Code Playgroud) 我是Java的新手,负责修复bug,问题如下.如果您提出建议/想法是什么问题以及如何解决这个问题,那将是非常好的:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:453)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
root cause
javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.CustMaint.Jsp.ProfProfileDetails_jsp._jspService(ProfProfileDetails_jsp.java:4016)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
root cause
java.lang.OutOfMemoryError
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.17
Run Code Online (Sandbox Code Playgroud) 我在将大型数据集加载到Jena时遇到了堆错误.`有没有办法可以为JVM(Java)分配一个大的堆空间.
我知道我可以通过更改eclipse.ini来实现这一点.但是在某种程度上我可以使用linux中的命令行来增加Java堆大小(我使用64GB RAM服务器:运行12.04 Ubuntu LTS服务器)?
由于堆空间较少而导致的错误是:超出了线程主gc开销限制的异常.
另外,我如何找到可以为系统设置的最大堆空间量
我尝试导出JAVA_OPTS = -Xms4096m -Xmx4096m,但最终得到错误:bash:export:-Xms4096m': not a valid identifier
bash: export:-Xmx4096m':不是有效的标识符
可能重复:
java.lang.OutOfMemoryError:Java堆空间
如何处理"java.lang.OutOfMemoryError:Java堆空间"错误(64MB堆大小)
你能告诉我怎样才能增加java内部堆内存?
我试图通过填充超过百万行来计算Arraylist和Linkedlist,并在Arraylist人口之后得到以下错误,
线程"main"中的异常java.lang.OutOfMemoryError:java.lang.Integer.valueOf(Integer.java:642)中的Java堆空间at scratch.Collectionss.main(Collectionss.java:25)
如何避免此错误,我尝试设置l1 = null但是这给了我一个错误,
public class Collectionss {
public static void main(String[] args){
//
long starttime = System.currentTimeMillis();
List<Integer> l1 = new ArrayList<Integer>();
for (int i = 1; i <= 10000000; i++){
l1.add(i);
}
System.out.println(l1.size());
long endtime = System.currentTimeMillis();
System.out.println(endtime - starttime);
//
long starttime1 = System.currentTimeMillis();
List<Integer> l2 = new LinkedList<Integer>();
for (int i = 1; i <= 10000000; i++){
l2.add(i);
}
System.out.println(l2.size());
long endtime1 = System.currentTimeMillis();
System.out.println(endtime1 - starttime1);
}
}
Run Code Online (Sandbox Code Playgroud) 我必须int input[]根据配置参数的高度和宽度为数组分配空间.
int input[]=new int[height * width]; //this is line no 538
Run Code Online (Sandbox Code Playgroud)
其中一个配置有参数height=8192和width=8192.所以数组的大小就变成了67108864.但是当我这样做时,我得到OutOfMemoryError.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Test.main(Test.java:538)
Run Code Online (Sandbox Code Playgroud)
我已经在eclipse以及cygwin上运行了这个程序,但我遇到了同样的问题.我认为这不是错误,也不是例外.我怎么能纠正这个?
我想从 URL 下载文件。文件大小为 564.31MB。我不知道这里发生了什么错误。另外,我想知道我的代码是否是从 URL 下载文件的正确方法。如果有更好的方法,请详细告诉我为什么比这个更好。谢谢。
import org.apache.commons.io.FilenameUtils;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by lukas on 6/30/16.
*/
public class Main {
public static void main(String[] args){
try {
String link = "https://s.basketbuild.com/uploads/devs/dianlujitao/oneplus3/cm13/cm-13.0-20160621-UNOFFICIAL-oneplus3.zip";
URL url = new URL(link);
InputStream inputStream = new BufferedInputStream(url.openStream());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int n=0;
byte[] buf = new byte[1024];
long duration = System.currentTimeMillis();
while((n=inputStream.read(buf))!=-1){
byteArrayOutputStream.write(buf, 0, n);
}
duration = System.currentTimeMillis()-duration;
System.out.println("Finish in "+duration+"ms");
inputStream.close();
File dir = new File("Output …Run Code Online (Sandbox Code Playgroud)