??????????????????????棿
?????????????????????
????A byte buffer is either direct or non-direct. Given a direct byte buffer?? the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is?? it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.
????byte byffer????????????????????????????棨???????棩???????????????棨??????棩??
???????????????????JVM??????IO????????и??????????????????????????????IO???????????????棬????????е????????????IO???????????????????棬?????????IO?????
?????????????????????????????????????????????
????????IO-->??????-->????????-->??????-->????IO
???????????????
????????IO-->??????-->????IO
???????????????IO??????????????緢?????????????????????и????Ч???
????A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap?? and so their impact upon the memory footprint of an application might not be obvious. It is therefore recommended that direct buffers be allocated primarily for large?? long-lived buffers that are subject to the underlying system's native I/O operations. In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.
???????????????????硣???????????????????allocateDirect?????????????????????????????????????????????????????????????JVM???????????????????????檔
????????????????к???????????棬???????????????????????????????????????檔????????????????????????????????????????????????????????檔
????????????????????????????????????????????????????????????????????????????????????????????ο????API ( ?????????ByteBuffer?????????)??????????????????????????Щ?÷????????????????
??????ó???
??????????????????????Щ???????????????????£??????????ó?????
????1 ?к???????????洢??????????????????
????2 ????????IO?????????????粢??????
??????????????????
????????????μ??????????????????????????
int time = 10000000;
Date begin = new Date();
for(int i=0;i<time;i++){
ByteBuffer buffer = ByteBuffer.allocate(2);
}
Date end = new Date();
System.out.println(end.getTime()-begin.getTime());
begin = new Date();
for(int i=0;i<time;i++){
ByteBuffer buffer = ByteBuffer.allocateDirect(2);
}
end = new Date();
System.out.println(end.getTime()-begin.getTime());
?????????????????£?

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