fastjson导致的OOM
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
大家好,我是烤鸭:
今天又遇到OOM了,原因在于 fastjson 的 toJSONString。
这是线上的报错信息。 将这个 TotalLoanDTO 对象 toJSONString 导致的OOM。日志如下。
2019-10-31 12:07:49.896 [TID:285.93.15724948639339801] [XNIO-1 task-14] INFO [class:com.sinoi logInfo==> 请求id:c8969c8a64c14a45b3079f218f7359a4,RPC调用服务异常,异常信息{}
com.alibaba.dubbo.rpc.service.GenericException: java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
at com.alibaba.fastjson.serializer.SerializeWriter.expandCapacity(SerializeWriter.java:
at com.alibaba.fastjson.serializer.SerializeWriter.writeFieldNameDirect(SerializeWriter
at com.alibaba.fastjson.serializer.ASMSerializer_22_TotalLoanDTO.write(Unknown Source)
at com.alibaba.fastjson.serializer.ListSerializer.write(ListSerializer.java:126)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:275)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:648)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:590)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:555)
at com.sinoiov.etc.aspect.ServiceLogAspect.getJson(ServiceLogAspect.java:98)
at com.sinoiov.etc.aspect.ServiceLogAspect.loggerInf(ServiceLogAspect.java:81)
at com.sinoiov.etc.aspect.ServiceLogAspect.aroundMethod(ServiceLogAspect.java:56)
at sun.reflect.GeneratedMethodAccessor266.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArg
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAsp
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMetho
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodIn
at com.sinoiov.etc.aspect.SaasServiceAspect.aroundMethod(SaasServiceAspect.java:78)
at sun.reflect.GeneratedMethodAccessor265.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArg
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAsp
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMetho
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocat
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMetho
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(
at com.sinoiov.etc.finance.service.impl.TotalLoanServiceImpl$$EnhancerBySpringCGLIB$$94
at com.sinoiov.etc.finance.api.impl.TotalLoanOperatorServiceImpl.queryTotalLoans(TotalL
at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:91)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.
at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.
at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleReque
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(He
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEven
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
本地写了个test类测试一下。
启动命令:设置最大内存 10M
VM options : -Xmx10M
package src.rams.oom;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
/**
* @Author test
* @Description //fastjson 测试 json的OOM
* @Date 2019/11/4 10:19
* @Param
* @return
*/
public class JsonOOM {
static class DumpTest{
}
public static void main(String[] args) throws Exception {
List<DumpTest> list = new ArrayList<>();
for (int i = 0; i < 54000; i++) {
DumpTest dumpTest = new DumpTest();
list.add(dumpTest);
}
String string = JSON.toJSONString(list);
System.out.println(string);
}
}
我们先看一下测试方法的报错信息。
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3664)
at java.lang.String.<init>(String.java:207)
at com.alibaba.fastjson.serializer.SerializeWriter.toString(SerializeWriter.java:425)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:650)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:590)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:555)
at src.rams.oom.JsonOOM.main(JsonOOM.java:31)
跟随源码看一下。
SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);
// 初始化char 数组
protected char buf[] = new char[2048];
后续的 toJSONString 。
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
最后报在这一行:
char[] copy = new char[newLength];
由于数组较大,直接申请分配堆内存上的空间,导致堆内存溢出。
再看一下生产上的OOM事件。同样的地方,都是在申请新的字符数组的时候导致的OOM。
public void expandCapacity(int minimumCapacity) {
int newCapacity = (buf.length * 3) / 2 + 1;
if (newCapacity < minimumCapacity) {
newCapacity = minimumCapacity;
}
char newValue[] = new char[newCapacity];
System.arraycopy(buf, 0, newValue, 0, count);
buf = newValue;
}
总结:
控制内存大小和json序列化对象的大小。如果有一个较大的list 去 toJsonString的话,占用的字符串长度不够用,会申请多余的字符数组,直到够用。如果有一个几万对象的list去toJsonString的话,那估计药丸。
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献6条内容
所有评论(0)