Coherence对象压缩以及对象大小计算
1.通过util.zip带的gzip压缩程序
Coherence对象压缩程序如下
|
package coherencetest; import com.tangosol.net.CacheFactory; import java.util.zip.*; public class CompressObject { public static byte[] writeCompressObject(Person object) public static Person readCompressObject(byte[] data_) return(object_); public static void main (String [] args) { Person person = new Person(); writeObjectToFile("zipcompress_before",person); //System.out.println(person); writeBytesToFile("zipcompress_after",compressperson); String personstr = new String(compressperson); NamedCache cache = CacheFactory.getCache("POFSample"); } byte[] a = (byte[]) cache.get("1"); //cache.retrieveCache(); public static void writeObjectToFile(String Filename, Object obj) { try { FileOutputStream outStream = new FileOutputStream("c:/"+Filename); // if file doesnt exists, then create it public static void writeBytesToFile(String Filename, byte[] objs) { try { FileOutputStream outStream = new FileOutputStream("c:/"+Filename); // if file doesnt exists, then create it } |
测试了一下,原来的对象3.96kb,然后经过压缩以后2.01K,还是有减少。
但通过Coherence的visualvm看了一下,发现平均对象大小是1byte.难道coherence只存放对象的指针吗?
2.通过kryo的序列化方法
|
package coherencetest; import com.esotericsoftware.kryo.Kryo; import com.tangosol.dev.assembler.New; import java.io.File; import org.objenesis.strategy.SerializingInstantiatorStrategy; import java.io.IOException; public class kryoCompress { public kryoCompress() { public static void main(String[] args) { Person person = new Person(); writeObjectToFile("person2",person); Kryo kryo = new Kryo(); byte[] scbytes = null; Person backsc =kryodeserialize(scbytes,kryo); } public static byte[] kryocompress(Person object, Kryo kryo) throws IOException { byte[] buffer = new byte[BYTES_LENTH]; Output out = new Output(buffer); kryo.writeObject(out, object); //System.out.println("kryocompress====total:"+out.total()); return out.toBytes(); } public static Person kryodeserialize(byte[] bytes,Kryo kryo) { try { input = new Input(bytes); return (Person)kryo.readObject(input, Person.class); } public static void writeObjectToFile(String Filename, Object obj) { try { FileOutputStream outStream = new FileOutputStream("c:/"+Filename); // if file doesnt exists, then create it public static void writeBytesToFile(String Filename, byte[] objs) { try { FileOutputStream outStream = new FileOutputStream("c:/"+Filename); // if file doesnt exists, then create it } |
测试了一下,原来标准的3.96K的对象,经过kryo序列化后变成了3.01K.可见应该只是序列化上的优化,压缩率比较小.
计算Coherence对象大小的程序
另外附上一个计算coherence对象大小的程序
|
package coherencetest; import java.text.DecimalFormat; import javax.management.MBeanServer; import com.tangosol.net.CacheFactory; import java.io.IOException; import javax.management.MBeanServerConnection; public class CalculateTheSizeOfPeopleCache { @SuppressWarnings({ "unchecked", "rawtypes" }) // Enable JMX support in this Coherence data grid session... // Create a sample cache just to access the data grid... // Gets the JMX server from Coherence data grid... //MBeanServerConnection jmxServer = getJMXServer(); // Creates a internal data structure that would maintain // Updates the internal data structure with statistic data // Finally... print the objects from the internal data } private class Statistics { private long unit; public Statistics(String cacheName) { public void incrementUnit(long unit) { public void incrementSize(long size) { public long getUnit() { public long getSize() { public double getUnitInMB() { public double getAverageSize() { public String toString() { } public static void main(String[] args) throws Exception { public static final DecimalFormat FORMAT = new DecimalFormat("###.###"); } |
需要的条件是:
- 不能以Coherence Extend Client的方式连入集群
- 需要开启-Dtangosol.coherence.management.remote=true -Dtangosol.coherence.management=all 参数
- 需要和cluster环境保持一直,生产就是生产,开发就是开发,-Dtangosol.coherence.mode=prod
输出如下:
Cache Statistics of 'POFSample':
- Total Entries of Cache -----> 10001
- Used Memory (Bytes) --------> 10001
- Used Memory (MB) -----------> 0.01
- Object Average Size --------> 1
Coherence对象压缩以及对象大小计算的更多相关文章
- Java对象的内存布局以及对象所需内存大小计算详解
1. 内存布局 在HotSpot虚拟机中,对象的内存布局可以分为三部分:对象头(Header). 实例数据(Instance Data)和对齐填充(Padding). 1) 对象头(Header): ...
- Java对象大小计算
这篇说说如何计算Java对象大小的方法.之前在聊聊高并发(四)Java对象的表示模型和运行时内存表示 这篇中已经说了Java对象的内存表示模型是Oop-Klass模型. 普通对象的结构如下,按64位机 ...
- JVM —— Java 对象占用空间大小计算
零. 为什么要知道 Java 对象占用空间大小 缓存的实现: 在设计 JVM 内缓存时(不是借助 Memcached. Redis 等), 须要知道缓存的对象是否会超过 JVM 最大堆限制, 假设会超 ...
- java对象在内存的大小
前言 一直以来,对java对象大小的概念停留在基础数据类型,比如byte占1字节,int占4字节,long占8字节等,但是一个对象包含的内存空间肯定不只有这些. 假设有类A和B,当new A()或者n ...
- 获取JAVA对象占用的内存大小
介绍两种获取JAVA对象内存大小的方法. 第一种:Instrumentation 简介: 使用java.lang.instrument 的Instrumentation来获取一个对象的内存大小.利用I ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少大对象堆的碎片,在某些情况下强制执行完整GC,按需压缩大对象堆,在GC前收到消息通知,使用弱引用缓存对象
减少大对象堆的碎片 如果不能完全避免大对象堆的分配,则要尽量避免碎片化. 对于LOH不小心就会有无限增长,但LOH使用的空闲列表机制可以减轻增长的影响.利用这个空闲列表,我们可以在两块分配区域中间找到 ...
- 如何获取一个Java对象所占内存大小
新建一个maven工程 我们先在IDEA中新建一个名为ObjectSizeFetcherAgent的maven工程,如下图: 在maven项目中的pom.xml中新增一个打jar包的插件,如下: &l ...
- 4种方法教你如何查看java对象所占内存大小
摘要:本文讲述4种查看java对象所占内存大小的方法 本文分享自华为云社区<查看java对象所占内存大小>,作者:xiewenci. 计算java对象所占内存大小 1.使用jdk8自带AP ...
- js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象
/** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...
随机推荐
- AE特效-与MAYA的结合、制作音乐舞蹈太极动作
http://blog.sina.com.cn/s/blog_a439a2670101fbkk.html AE特效-与MAYA的结合.制作音乐舞蹈太极动作 (2013-07-24 14:44:12) ...
- locust===注意事项
1.安装包在:微盘 2.运行命令是:locust -f load_test.py --host=https://www.baidu.com 3.本地打开的是:http://localhost:8089 ...
- linux基础了解的学习记录
一.文件结构图 linux的储存结构为文件树 二.绝对路径.相对路径.权限 1.绝对路径: /usr/local/include 在路径的最前面是 / 开头的 使用环境:当在当前路径下想到 ...
- ECharts问题--散点图中对散点添加点击事件
1. 我们这次就没有先讲解怎么使用散点图了,这个跟之前的一些图还是很类似的,不会的可以去官网上面查看 API 使用.我们这次讲解的是为散点图中的散点添加点击事件,然后在图表之外的一个 div 里面显示 ...
- 只运行一个exe应用程序的使用案例
应用程序的exe启动设置 using System;using System.Diagnostics;using System.Reflection;using System.Runtime.Inte ...
- 错误: 在类 Main 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
错误: 在类 Main 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args)否则 JavaFX 应用程序类必须扩展ja ...
- Selenium2+python自动化73-定位的坑:class属性有空格【转载】
前言 有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn ...
- iptables内网地外网之间访问
环境:一台带外网和内网的机器,另一台只有内网,默认不能上网.两台机器都是centos系统带外网机器的外网ip为 123.221.20.11, 内网网关ip为 192.168.15.100内网机器的内网 ...
- Jest+Enzyme React js/typescript测试环境配置案例
本文案例github:https://github.com/axel10/react-jest-typescript-demo 配置jest的react测试环境时我们可以参考官方的配置教程: http ...
- 【转】Python模块subprocess
subprocess 早期的Python版本中,我们主要是通过os.system().os.popen().read()等函数.commands模块来执行命令行指令的,从Python 2.4开始官方文 ...