开发者社区> 问答> 正文

protostuff序列化java对象时竟然比java原生序列化还占内存?报错

今天测试序列化对象的时候循环10000次下列对象 protostuff报错

而java的序列化不但没报错而且速度一直比proto快

下面是实体类,和具体序列化方法

展开
收起
爱吃鱼的程序员 2020-06-08 20:05:08 588 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    java原生序列化是最稳定的,我用过kryo,执行10000次的效率比java原生的好,但是多了会有不知名的错误

    <aname="top"rel="nofollow">

    可参考jvmserializers的一些对比结果,在我这边的使用过程中,protostuff的序列化结果占空间很小(1.3.8版本 ),其中创建Schema估计比较费时,可以考虑重用。另外一个受影响的是LinkedBuffer的allocate。Java原始的<spanstyle="font-size:13.3333px;">serializer无疑是最耗费空间的,请检查下benchmark代码是否有问题,再出结论。回复<aclass="referer"target="_blank">@Rcd:贴到了回答区回复<aclass="referer"target="_blank">@Mr_SWang:并非大神,请以客观冷静的眼光看待问题。protobuff协议本身是非常节省存储空间的,经过大量生产环境验证,可放代码大家看看是否有问题。您好大神,我上面的是嵌套数据对象,并且将数据构造完成之后用protostuff两行代码来做序列化难道我要改源码?
    <spanstyle="color:#000080;font-weight:bold;">intsize=<spanstyle="color:#0000ff;">100;Map<String,String>childMap=<spanstyle="color:#000080;font-weight:bold;">newHashMap<String,String>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childMap.put(i+<spanstyle="color:#008000;font-weight:bold;">"",<spanstyle="color:#008000;font-weight:bold;">"mapvalue:"+i);}List<String>chilList=<spanstyle="color:#000080;font-weight:bold;">newArrayList<String>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){chilList.add(<spanstyle="color:#008000;font-weight:bold;">"listvalue:"+i);}SerializerEntityChildchildEntity=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntityChild();childEntity.setBoolvalue(<spanstyle="color:#000080;font-weight:bold;">true);childEntity.setMap(childMap);childEntity.setList(chilList);List<SerializerEntityChild>childList=<spanstyle="color:#000080;font-weight:bold;">newArrayList<SerializerEntityChild>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childEntity.setIntvalue(i);childEntity.setLongvalue(i);childEntity.setKey(<spanstyle="color:#008000;font-weight:bold;">"key:"+i);childList.add(childEntity);}SerializerEntityentity=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntity();Map<String,SerializerEntityChild>map=<spanstyle="color:#000080;font-weight:bold;">newHashMap<String,SerializerEntityChild>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childEntity.setIntvalue(i);childEntity.setLongvalue(i);childEntity.setKey(<spanstyle="color:#008000;font-weight:bold;">"key:"+i);map.put(i+<spanstyle="color:#008000;font-weight:bold;">"",childEntity);}entity.setList(childList);entity.setMap(map);entity.setKey(<spanstyle="color:#008000;font-weight:bold;">"main");entity.setBoolvalue(<spanstyle="color:#000080;font-weight:bold;">true);entity.setIntvalue(<spanstyle="color:#0000ff;">1);

    entity.setLongvalue(<spanstyle="color:#0000ff;">1);

    Schema<SerializerEntity>schemal=RuntimeSchema.<spanstyle="font-style:italic;">createFrom(SerializerEntity.<spanstyle="color:#000080;font-weight:bold;">class);st=System.<spanstyle="font-style:italic;">currentTimeMillis();<spanstyle="color:#000080;font-weight:bold;">byte[]prob=ProtostuffIOUtil.<spanstyle="font-style:italic;">toByteArray(entity,schemal,LinkedBuffer.<spanstyle="font-style:italic;">allocate(LinkedBuffer.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">MIN_BUFFER_SIZE));System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"序列化大小:"+prob.<spanstyle="color:#660e7a;font-weight:bold;">length);System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"用时--------------------"+(System.<spanstyle="font-style:italic;">currentTimeMillis()-st));System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"反序列化------------------");st=System.<spanstyle="font-style:italic;">currentTimeMillis();SerializerEntitynewent=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntity();ProtostuffIOUtil.<spanstyle="font-style:italic;">mergeFrom(prob,newent,schemal);System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"用时--------------------"+(System.<spanstyle="font-style:italic;">currentTimeMillis()-st));

    2020-06-08 20:05:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载