spring | handle大型json | jvm outofmemory

voase2hg  于 2021-07-15  发布在  Java
关注(0)|答案(0)|浏览(171)

我的问题是如何处理传入spring webapp的大型json文件。
我的控制器方法如下:

@RequestMapping(value = "/**", method = POST)
    public ResponseEntity<? extends Object> process(RequestData requestData, @RequestBody(required = false) String requestBody) {

当我发送这样的文件时: curl -X POST -H "Content-Type: application/json" -d @test.json http://localhost:40080/myapp/api 我有:

16:58:27.026|http-listener-1(2)|ERROR|ExceptionHandler|Exception has been thrown: 
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception
is java.lang.OutOfMemoryError: Java heap space         at 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.2.0.RELEASE.jar :5.2.0.RELEASE]         at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.0.RELEASE.jar:5 .2.0.RELEASE]         at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.0.RELEASE.java

好的,我可以操作xmx和xms参数,但是有没有办法告诉spring不要一次读取所有@requestbody?这些情况下的最佳实践是什么?
现在我的设置是“-xmx512m”和“-xms256m”,当我发布70mb文件时仍然有问题,为什么?
gc日志显示:

Heap before GC invocations=604 (full 96):
 PSYoungGen      total 131072K, used 13487K [0x00000000f5580000, 0x00000000ff800000, 0x0000000100000000)
  eden space 97792K, 0% used [0x00000000f5580000,0x00000000f5580000,0x00000000fb500000)
  from space 33280K, 40% used [0x00000000fd780000,0x00000000fe4abf08,0x00000000ff800000)
  to   space 34304K, 0% used [0x00000000fb500000,0x00000000fb500000,0x00000000fd680000)
 ParOldGen       total 349696K, used 268235K [0x00000000e0000000, 0x00000000f5580000, 0x00000000f5580000)
  object space 349696K, 76% used [0x00000000e0000000,0x00000000f05f2e40,0x00000000f5580000)
 Metaspace       used 231535K, capacity 248408K, committed 267160K, reserved 1282048K
  class space    used 27227K, capacity 31600K, committed 34992K, reserved 1048576K
2021-03-26T17:41:15.154+0100: 13343.695: [Full GC (JvmtiEnv ForceGarbageCollection) [PSYoungGen: 13487K->0K(131072K)] [ParOldGen: 268235K->191766K(349696K)] 281723K->191766K(480768K), [Metaspace: 231535K->231060K(1282048K)], 1.1538288 secs] [Times: user=2.64 sys=0.01, real=1.16 secs]

伊甸园空间是97792k,青年131072k-为什么不是足够的70米文件-假设它是一个新的对象?头顶Spring?
顺便问一下,为什么“伊甸园+从+到!=“年轻人”?
编辑:psyounggen不是“eden”、“from”和“to”的总和吗?
运行:java hotspot(tm)64位服务器vm(内部版本25.171-b11,混合模式)/glassfish 4.1/spring 5.2
编辑2:这可能会有所帮助-当oom发生时,我进行了堆转储,并发现以下堆栈跟踪:

java.lang.OutOfMemoryError.<init>() (line: 48)
java.util.Arrays.copyOf(char[ ], int) (line: 3332)
java.lang.AbstractStringBuilder.ensureCapacityInternal(int) (line: 124)
java.lang.AbstractStringBuilder.append(char[ ], int, int) (line: 596)
java.lang.StringBuilder.append(char[ ], int, int) (line: 190)
org.springframework.util.StreamUtils.copyToString(java.io.InputStream, java.nio.charset.Charset) (line: 90)
org.springframework.http.converter.StringHttpMessageConverter.readInternal(java.lang.Class, org.springframework.http.HttpInputMessage) (line: 94)
org.springframework.http.converter.StringHttpMessageConverter.readInternal(java.lang.Class, org.springframework.http.HttpInputMessage) (line: 44)
org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class, org.springframework.http.HttpInputMessage) (line: 199)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(org.springframework.http.HttpInputMessage, org.springframework.core.MethodParameter, java.lang.reflect.Type) (line: 206)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(org.springframework.web.context.request.NativeWebRequest, org.springframework.core.MethodParameter, java.lang.reflect.Type) (line: 158)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory) (line: 131)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory) (line: 121)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(org.springframework.web.context.request.NativeWebRequest, org.springframework.web.method.support.ModelAndViewContainer, java.lang.Object[ ]) (line: 167)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(org.springframework.web.context.request.NativeWebRequest, org.springframework.web.method.support.ModelAndViewContainer, java.lang.Object[ ]) (line: 134)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(org.springframework.web.context.request.ServletWebRequest, org.springframework.web.method.support.ModelAndViewContainer, java.lang.Object[ ]) (line: 106)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.web.method.HandlerMethod) (line: 888)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.web.method.HandlerMethod) (line: 793)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) (line: 87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) (line: 1040)
org.springframework.web.servlet.DispatcherServlet.doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) (line: 943)
org.springframework.web.servlet.FrameworkServlet.processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) (line: 1006)
org.springframework.web.servlet.FrameworkServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) (line: 909)
javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) (line: 707)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题