1、一个大的对象文件allFiles(是Vector),在allFiles中存放的又都是些Vecor对象(这个vector中存放的也都是对象)来的。我要将其按存储容量均分或者大概均分为几个文件,翻遍了API没找到合适的方法。
用Vector的capacity()了,它返回的是这个Vector里面存储内容的字节大小,可如果我这个Vector里又是对象时,它记载的是这个对象的引用的,这样就没有记录这个大Vector的真正存储大小了。
2、寻找一个工具。要写一个在程序中建库建表的东东,这样的东东我觉得肯定网上有人弄的吧,搜了几搜都没找到。想到要给每个表定义字段,字段类型,再生成一堆sql就觉得写起来麻烦。不知各位谁知道有这样别人写好的工具类不,有的话不省的再去写了。关键是第一个问题,第二个实在没有就自己写。
谢谢各位!
用Vector的capacity()了,它返回的是这个Vector里面存储内容的字节大小,可如果我这个Vector里又是对象时,它记载的是这个对象的引用的,这样就没有记录这个大Vector的真正存储大小了。
2、寻找一个工具。要写一个在程序中建库建表的东东,这样的东东我觉得肯定网上有人弄的吧,搜了几搜都没找到。想到要给每个表定义字段,字段类型,再生成一堆sql就觉得写起来麻烦。不知各位谁知道有这样别人写好的工具类不,有的话不省的再去写了。关键是第一个问题,第二个实在没有就自己写。
谢谢各位!
可能对你有帮助。
--
1.按照描述的对象管理形式已经是树状,即使有一个动态计算对象大小的api,也要遍历tree去统计,比较麻烦。
另外这种平均分配是依赖于文件尺寸,即持久化以后的结果。那么就是说,这个api必须有能力计算对象持久化以后占用介质存储空间的能力。
我不了解持久化的更详细的细节,但可以肯定对象的内存尺寸和对象持久化后占用介质尺寸天壤之别2.就不用说了,论简单而具有易用性,参考access查询设计器
2、这几天有点时间了,也在网上找了找,还真没发现有JAVA写成的类似的包,看来还得自己来写了,郁闷。
import java.util.Collection;/**
* Utility class for determining the sizes in bytes of commonly used objects.
* Classes implementing the Cacheable interface should use this class to
* determine their size.
*
* @author Matt Tucker
*/
public class CacheSizes { /**
* Returns the size in bytes of a basic Object. This method should only
* be used for actual Object objects and not classes that extend Object.
*
* @return the size of an Object.
*/
public static int sizeOfObject() {
return 4;
} /**
* Returns the size in bytes of a String.
*
* @param string the String to determine the size of.
* @return the size of a String.
*/
public static int sizeOfString(String string) {
if (string == null) {
return 0;
}
return 4 + string.length() * 2;
} /**
* Returns the size in bytes of a primitive int.
*
* @return the size of a primitive int.
*/
public static int sizeOfInt() {
return 4;
} /**
* Returns the size in bytes of a primitive char.
*
* @return the size of a primitive char.
*/
public static int sizeOfChar() {
return 2;
} /**
* Returns the size in bytes of a primitive boolean.
*
* @return the size of a primitive boolean.
*/
public static int sizeOfBoolean() {
return 1;
} /**
* Returns the size in bytes of a primitive long.
*
* @return the size of a primitive long.
*/
public static int sizeOfLong() {
return 8;
} /**
* Returns the size in bytes of a primitive double.
*
* @return the size of a primitive double.
*/
public static int sizeOfDouble() {
return 8;
} /**
* Returns the size in bytes of a Date.
*
* @return the size of a Date.
*/
public static int sizeOfDate() {
return 12;
} /**
* Returns the size in bytes of a Map object. All keys and
* values <b>must be Strings</b>.
*
* @param map the Map object to determine the size of.
* @return the size of the Map object.
*/
public static int sizeOfMap(Map map) {
if (map == null) {
return 0;
}
// Base map object -- should be something around this size.
int size = 36;
// Add in size of each value
Object[] values = map.values().toArray();
for (int i = 0; i < values.length; i++) {
size += sizeOfString((String)values[i]);
}
Object[] keys = map.keySet().toArray();
// Add in each key
for (int i = 0; i < keys.length; i++) {
size += sizeOfString((String)keys[i]);
}
return size;
} /**
* Returns the size in bytes of a Collection object. Elements are assumed to be
* <tt>String</tt>s, <tt>Long</tt>s or <tt>Cacheable</tt> objects.
*
* @param list the Collection object to determine the size of.
* @return the size of the Collection object.
*/
public static int sizeOfCollection(Collection list) {
if (list == null) {
return 0;
}
// Base list object (approximate)
int size = 36;
// Add in size of each value
Object[] values = list.toArray();
for (int i = 0; i < values.length; i++) {
Object obj = values[i];
if (obj instanceof String) {
size += sizeOfString((String)obj);
}
else if (obj instanceof Long) {
size += sizeOfLong() + sizeOfObject();
}
else {
size += ((Cacheable)obj).getCachedSize();
}
}
return size;
}
}
size += ((Cacheable)obj).getCachedSize();
}请问这个Cacheable是个什么类?在MAP中。
Object[] values = map.values().toArray();
for (int i = 0; i < values.length; i++) {
size += sizeOfString((String)values[i]);
}
其实MAP中放的很多东西都不是String的,这样写是得不到实际的size的。