RestAssured.get(url).statusCode导致java.lang.OutOfMemoryError:Java堆空间

时间:2019-07-05 18:38:50

标签: java httpresponse rest-assured

我正在使用RestAssured.get(URL).statusCode()来查看URL是否正常工作并成功响应(代码200)。在某些网址(如url1)的情况下,它可以正常工作,但对于某些网址(如url2),则会导致以下错误:

String url1="http://www.bbsys.com/playersdemo.exe"
String url2="http://www.ezhomeinspectionsoftware.com/filestorage/EZ-Setup.exe"
这两个网址

都导致下载文件。但是第二个下载更大的文件。因此,这可能就是导致内存不足异常的原因。我需要知道如何处理这种情况。我需要从大量不同的URL中获得响应。您是建议使用其他库还是我以错误的方式使用该库?

 java.lang.OutOfMemoryError: Java heap space 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3236)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
    at io.restassured.internal.util.IOUtils.toByteArray(IOUtils.java:31)
    at io.restassured.internal.http.GZIPEncoding$GZIPDecompressingEntity.getContent(GZIPEncoding.java:69)
    at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:85)
    at io.restassured.internal.http.HTTPBuilder.parseResponse(HTTPBuilder.java:545)
    at io.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.super$2$parseResponse(RequestSpecificationImpl.groovy)
    at sun.reflect.GeneratedMethodAccessor108.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:132)
    at io.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.parseResponse(RequestSpecificationImpl.groovy:2137)
    at sun.reflect.GeneratedMethodAccessor107.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
    at io.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.doRequest(RequestSpecificationImpl.groovy:2073)
    at io.restassured.internal.http.HTTPBuilder.doRequest(HTTPBuilder.java:494)
    at io.restassured.internal.http.HTTPBuilder.request(HTTPBuilder.java:451)
    at io.restassured.internal.http.HTTPBuilder$request$2.call(Unknown Source)
    at io.restassured.internal.RequestSpecificationImpl.sendHttpRequest(RequestSpecificationImpl.groovy:1450)
    at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)

2 个答案:

答案 0 :(得分:0)

OOM或OOME(OutOfMemoryError)仅表示JVM内存不足。发生这种情况时,您基本上有2个选择:

  1. 使用-Xmx VM参数允许JVM使用更多内存。例如,要允许JVM使用1 GB(1024 MB)的内存,请执行以下操作:
  2. 改进/修复该应用程序,使其使用更少的内存。 有关更多信息,请通过此链接

http://www.mkyong.com/eclipse/eclipse-java-lang-outofmemoryerror-java-heap-space/

答案 1 :(得分:0)

您实际上是要下载文件还是只想检查URL是否有效?

如果不接受,请查看head方法(https://static.javadoc.io/io.rest-assured/rest-assured/3.0.7/io/restassured/RestAssured.html#head-java.lang.String-java.util.Map-),以发送头请求而不触发下载。

否则,唯一的选择就是提供足够的内存,如@Roberto建议的那样。