treeroot(根根) ( ) 你知道怎么做吗?
解决方案 »
- java的跨平台到底是什么意思?
- 这道题该怎么写啊,我写了一点就卡住了
- 如何用反射来掉对象的方法
- 父类域(field)的访问修饰【private或protected】用哪个更好?
- 请教两个用jx1读取EXCEL的问题
- 我想做一个曲线走势图,就是读取数据库,然后画一个走势图出来,请问界面和曲线如何画?
- JFrame和JDialog有何区别?
- 大家帮我看看这段代码:它是如何得到name和phone的值,并写到文件中取得
- 如何做个搜索引擎?
- org.jvnet.substance的问题
- java des加密后多出8个字节,急急急,300分相送,绝不食言
- JDK1.5在adm+64位linux上的测试结果, 太意外了!
我想应该用类型相加。
跟这个有关么?
list.size()*一个reference所占的字节数,就是一个List对象所占的内存
不过用java到现在,没有遇见过在内存中所占字节数的问题!
[email protected]
谢谢^-^
[email protected]
我怕收不到
import java.util.*;/**
* Compute the size of a Runtime object or Primitive type.
* Because the memory allocation is unknown,we just add
* all the fields size of a object.
* the primitive size is thounght as
* boolean:1
* byte:1
* short:2
* char:2
* int:4
* float:4
* long:8
* double:8
* the array size are the sum of all the elements
* the null reference is thought to 4 byte.
* the Object instance have the size 8
*
*/
public class Size
{
final private static int BOOLEAN=1;
final private static int BYTE=1;
final private static int SHORT=2;
final private static int CHAR=2;
final private static int INT=4;
final private static int FLOAT=4;
final private static int LONG=8;
final private static int DOUBLE=8;
final private static int REFERENCE=4;
final private static int OBJECT=8;
private static ThreadLocal objs=new ThreadLocal();
private static void init(Object o){
Map map=new IdentityHashMap();
map.put(o,null);
objs.set(map);
} public static int sizeof(boolean i){
return BOOLEAN;
}
public static int sizeof(byte b){
return BYTE;
}
public static int sizeof(short s){
return SHORT;
}
public static int sizeof(char c){
return CHAR;
}
public static int sizeof(int i){
return INT;
}
public static int sizeof(float f){
return FLOAT;
}
public static int sizeof(long l){
return LONG;
}
public static int sizeof(double d){
return DOUBLE;
}
public static int sizeof(Object o) throws IllegalAccessException{
init(o);
return sizeof0(o);
}
private static int sizeof0(Object o) throws IllegalAccessException{
int size=OBJECT;
//if the object is null
if(o==null) return REFERENCE;
Map map=(Map)objs.get();
Class c=o.getClass(); //if it is array
if(c.isArray()){
int[] dimension=getDimension(o);
int len=dimension.length;
Object obj=o;
int num=1;
for(int j=0;j<len-1;j++) num*=dimension[j];
if(dimension[len-1]==0){
size+=num*REFERENCE;
}
else{
num*=dimension[len-1];
//处理递归
int[] index;
Class type=c;
while(type.isArray()) type=type.getComponentType();
//基本类型的数组
if(type.isPrimitive()){
size+=num*sizeofPrimitive(type);
}
//引用类型数组
else{
for(int k=0;k<num;k++){
index=countToIndex(k,dimension);
Object temp=obj;
for(int m=0;m<len;m++){
temp=Array.get(temp,index[m]);
}
//加入数组中的所有对象
if(!map.containsKey(temp)){
size+=sizeof0(temp);
map.put(temp,null);
}
}
}
}
}
// all not-static fields
Field[] fs=getFields(o.getClass()); for(int i=0;i<fs.length;i++){
Field f=fs[i];
if(!Modifier.isStatic(f.getModifiers())){ Class type=f.getType();
//if it is primitive
if(type.isPrimitive()){
size+=sizeofPrimitive(type);
}
//recurtive
else{
Object obj=f.get(o);
if(!map.containsKey(obj)){
size+=sizeof0(obj);
map.put(obj,null);
}
}
}
}
return size;
} private static int[] countToIndex(int count,int[] d){
int[] res=new int[d.length];
int c=count;
int i=1;
while(c>0){
int t=1;
for(int j=i;j<d.length;j++) t*=d[j];
if(t>c) i++;
else{
res[i-1]=c/t;
c=c%t;
}
}
return res;
}
private static int sizeofPrimitive(Class c){
if (c==boolean.class){
return BOOLEAN;
}
else if(c==byte.class){
return BYTE;
}
else if(c==char.class){
return CHAR;
}
else if(c==short.class){
return SHORT;
}
else if(c==int.class){
return INT;
}
else if(c==float.class){
return FLOAT;
}
else if(c==long.class){
return LONG;
}
else if(c==double.class){
return DOUBLE;
}
else{
throw new IllegalArgumentException("Thrown by sizeOfPrimitive()");
}
}
private static int[] getDimension(Object obj) throws IllegalAccessException{
int dimension=0;
Class c=obj.getClass();
while(c.isArray()){
dimension++;
c=c.getComponentType();
}
int[] res=new int[dimension];
Object o=obj;
for(int i=0;i<dimension-1;i++){
res[i]=Array.getLength(o);
o=Array.get(o,0);
}
res[dimension-1]=Array.getLength(o); return res;
}
private static Field[] getFields(Class c){
Class superClass=c.getSuperclass();
Field[] s=null;
if(superClass!=null){
getFields(superClass);
}
Field[] fs=c.getDeclaredFields();
//设置为可访问的
Field.setAccessible(fs,true);
//合并
int size=0;
if(s!=null) size+=s.length;
if(fs!=null) size+=fs.length; Field[] result=new Field[size];
int index=0;
if((s!=null)&&(s.length>0)){
System.arraycopy(s,0,result,0,s.length);
index+=s.length;
}
if((fs!=null)&&(fs.length>0)){
System.arraycopy(fs,0,result,index,fs.length);
} return result;
}
}
import java.util.*;/**
* Compute the size of a Runtime object or Primitive type.
* Because the memory allocation is unknown,we just add
* all the fields size of a object.
* the primitive size is thounght as
* boolean:1
* byte:1
* short:2
* char:2
* int:4
* float:4
* long:8
* double:8
* the array size are the sum of all the elements
* the null reference is thought to 4 byte.
* the Object instance has the size 8.
*
*/
public class Size
{
final private static int BOOLEAN=1;
final private static int BYTE=1;
final private static int SHORT=2;
final private static int CHAR=2;
final private static int INT=4;
final private static int FLOAT=4;
final private static int LONG=8;
final private static int DOUBLE=8;
final private static int REFERENCE=4;
final private static int OBJECT=8;
private static ThreadLocal objs=new ThreadLocal();
private static void init(Object o){
Map map=new IdentityHashMap();
map.put(o,null);
objs.set(map);
} public static int sizeof(boolean i){
return BOOLEAN;
}
public static int sizeof(byte b){
return BYTE;
}
public static int sizeof(short s){
return SHORT;
}
public static int sizeof(char c){
return CHAR;
}
public static int sizeof(int i){
return INT;
}
public static int sizeof(float f){
return FLOAT;
}
public static int sizeof(long l){
return LONG;
}
public static int sizeof(double d){
return DOUBLE;
}
public static int sizeof(Object o){
init(o);
return sizeof0(o);
}
private static int sizeof0(Object o) {
int size=OBJECT;
//if the object is null
if(o==null) return REFERENCE;
Map map=(Map)objs.get();
Class c=o.getClass(); //if it is array
if(c.isArray()){
int[] dimension=getDimension(o);
int len=dimension.length;
Object obj=o;
int num=1;
for(int j=0;j<len-1;j++) num*=dimension[j];
if(dimension[len-1]==0){
size+=num*REFERENCE;
}
else{
num*=dimension[len-1];
//处理递归
int[] index;
Class type=c;
while(type.isArray()) type=type.getComponentType();
//基本类型的数组
if(type.isPrimitive()){
size+=num*sizeofPrimitive(type);
}
//引用类型数组
else{
for(int k=0;k<num;k++){
index=countToIndex(k,dimension);
Object temp=obj;
for(int m=0;m<len;m++){
temp=Array.get(temp,index[m]);
}
//加入数组中的所有对象
if(!map.containsKey(temp)){
size+=sizeof0(temp);
map.put(temp,null);
}
}
}
}
}
// all not-static fields
Field[] fs=getFields(o.getClass()); for(int i=0;i<fs.length;i++){
Field f=fs[i];
if(!Modifier.isStatic(f.getModifiers())){ Class type=f.getType();
//if it is primitive
if(type.isPrimitive()){
size+=sizeofPrimitive(type);
}
//recurtive
else{
Object obj=null;
try{
obj=f.get(o);
}
catch(IllegalAccessException e){
//won't be happen
throw new RuntimeException("impossible");
}
if(!map.containsKey(obj)){
size+=sizeof0(obj);
map.put(obj,null);
}
}
}
}
return size;
} private static int[] countToIndex(int count,int[] d){
int[] res=new int[d.length];
int c=count;
int i=1;
while(c>0){
int t=1;
for(int j=i;j<d.length;j++) t*=d[j];
if(t>c) i++;
else{
res[i-1]=c/t;
c=c%t;
}
}
return res;
}
private static int sizeofPrimitive(Class c){
if (c==boolean.class){
return BOOLEAN;
}
else if(c==byte.class){
return BYTE;
}
else if(c==char.class){
return CHAR;
}
else if(c==short.class){
return SHORT;
}
else if(c==int.class){
return INT;
}
else if(c==float.class){
return FLOAT;
}
else if(c==long.class){
return LONG;
}
else if(c==double.class){
return DOUBLE;
}
else{
throw new IllegalArgumentException("Thrown by sizeOfPrimitive()");
}
}
private static int[] getDimension(Object obj){
int dimension=0;
Class c=obj.getClass();
while(c.isArray()){
dimension++;
c=c.getComponentType();
}
int[] res=new int[dimension];
Object o=obj;
for(int i=0;i<dimension-1;i++){
res[i]=Array.getLength(o);
o=Array.get(o,0);
}
res[dimension-1]=Array.getLength(o); return res;
}
private static Field[] getFields(Class c){
Class superClass=c.getSuperclass();
Field[] s=null;
if(superClass!=null){
getFields(superClass);
}
Field[] fs=c.getDeclaredFields();
//设置为可访问的
Field.setAccessible(fs,true);
//合并
int size=0;
if(s!=null) size+=s.length;
if(fs!=null) size+=fs.length; Field[] result=new Field[size];
int index=0;
if((s!=null)&&(s.length>0)){
System.arraycopy(s,0,result,0,s.length);
index+=s.length;
}
if((fs!=null)&&(fs.length>0)){
System.arraycopy(fs,0,result,index,fs.length);
} return result;
}
}