when transfer object to json, with gson api "toJson", sometimes the time cost is much longer than usual

huangapple 未分类评论51阅读模式
英文:

when transfer object to json, with gson api "toJson", sometimes the time cost is much longer than usual

问题

使用gson在我的程序中将对象转换为json,一切正常。但当我进行性能测试时,我发现有时候调用"toJson(Object)"的耗时比平时要长得多。例如,通常情况下,转换所需时间不到1毫秒。当我调用此方法1000次时,有几次转换需要3或5毫秒,甚至超过10毫秒。在这段时间内,我检查了Tomcat的GC日志,没有发生minor gc或full gc。\n\n将程序简化为以下格式:\n\nfor(i=0;i<1000;i++){\n...\nmethod1();\nmethod2();\n... \nlong stamp1 =System.currentTimeMillis(); \nstring msg = gson.toJson(object); \nlong stamp2 =System.currentTimeMillis(); \nsystem.out.println(stamp2-stamp1); \nlog.info(msg);\n...\n}\n

英文:

I use gson in my program to transfer object to json, everything works. But when I do the performance test, I found that sometimes "toJson(Object)" cost much longer than usual. For example, usually, the transfer takes less than 1ms. When I invoke this method for 1000 times, there are several times the transfer takes 3 or 5ms, or even more than 10ms. During this period, I checked the tomcat GC log, no minor gc or full gc happened.
simplify the program into the following format:

for(i=0;i<1000;i++){
...
method1();
method2();
...  
long stamp1 =System.currentTimeMillis(); 
string msg = gson.toJson(object);   
long stamp2 =System.currentTimeMillis(); 
system.out.println(stamp2-stamp1); 
log.info(msg);
...
}

huangapple
  • 本文由 发表于 2020年7月27日 15:25:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/63110536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定