public static int KB = 1024;
public static int MB = 1024 * KB;
private static final int CAPACITY_SMALL = 4 * KB;
private static final int CAPACITY_MEDIUM = 128 * KB;
private static final int CAPACITY_LARGE = 1024 * KB;
//package scope (default) - so they can be accessed from unit tests.
byte[] smallMessageBuffer = new byte[1024 * 4 * KB]; //1024 x 4KB messages = 4MB.
byte[] mediumMessageBuffer = new byte[128 * 128 * KB]; // 128 x 128KB messages = 16MB.
byte[] largeMessageBuffer = new byte[16 * 1 * MB]; // 16 * 1MB messages = 16MB.
经常看见缓存的大小都是以位的大小定义,是因为习惯,还是因为什么原因?在实际项目中是否有什么策略根据自己需求来定义最优的值来处理IO?
缓存大小什么单位纯粹是个人喜好,最后都是化为以字节为单位。缓存越大,传输大数据量效率会更高,因为循环次数减少了;相应的如果每次实际传输的数据量大大小于缓存大小,比如只用到缓存大小的 1/10,那就有点浪费内存了,当然内存不敏感(比如多用个 1~2 MB 完全可以接受)的情况下无所谓。