??????????????????????

publicclassTestProxy{

publicstaticvoidmain(String[]args){

try{

Foofoo=(Foo)Handler.newInstance(newFooImpl());

foo.testArrayList();

foo.testLinkedList();

}catch(Exceptione){

e.printStackTrace();

}

}

}

???????械??????锟�?

beginmethodtestArrayList()

themethodtestArrayListlasts0ms

endmethodtestArrayList

beginmethodtestLinkedList()

themethodtestLinkedListlasts219ms

endmethodtestLinkedList

???????????????????????????写???FooImpl?????????????????貌?写??????????????????????????????

????4.3???

????????????????????????????????????????????????????????????渭????????斜?????????????????卸????胁???????????????睢�?煤??????????????????????“???????械????????????????????????????”??


????5??????????

????5.1???

?????????java??贸???????????泻????????????????????????????????????????????????????械???????????????????????????????????Runtime???freeMemory()??totalMemory()??????

????5.2???

?????????????????????????????????????????

classMemory

{

publicstaticlongused()

{

longtotal=Runtime.getRuntime().totalMemory();

longfree=Runtime.getRuntime().freeMemory();

return(total-free);

}

}

??????????Handler???invoke()??????

publicObjectinvoke(Objectproxy??Methodmethod??Object[]args)throwsThrowable{

Objectresult;

try{

System.out.print("beginmethod"+method.getName()+"(");

for(inti=0;args!=null&&i<args.length;i++){

if(i>0)System.out.print("??");

System.out.print(""+

args[i].toString());

}

System.out.println(")");

longstart=Memory.used();

result=method.invoke(obj??args);

longend=Memory.used();

System.out.println("memoryincreasedby"+(end-start)+"bytes");

}catch(InvocationTargetExceptione){

throwe.getTargetException();

}catch(Exceptione){

thrownewRuntimeException

("unexpectedinvocationexception:"+

e.getMessage());

}finally{

System.out.println("endmethod"+method.getName());

}

returnresult;

}