File tree Expand file tree Collapse file tree
src/main/java/com/sun/jvm/Jvm Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .sun .jvm .Jvm ;
2+
3+ import java .nio .ByteBuffer ;
4+
5+ /**
6+ * @ClassName AccessDirectBuffer
7+ * @Despacito TODO
8+ * @Author chenzhuo
9+ * @Version 1.0
10+ * 堆外内存的使用
11+ * 堆外内存要比直接使用堆内存快
12+ * 但是堆外内存要限制使用的量,要不就会oom
13+ **/
14+ public class AccessDirectBuffer {
15+ public void directAccess (){
16+ long startTime = System .currentTimeMillis ();
17+ testWrite ();
18+ long endTime = System .currentTimeMillis ();
19+ System .out .println ("testDirectWrite:" + (endTime - startTime ));
20+ }
21+ public void bufferAccess (){
22+ long startTime = System .currentTimeMillis ();
23+ testWrite ();
24+ long endTime = System .currentTimeMillis ();
25+ System .out .println ("testBufferWrite:" + (endTime - startTime ));
26+ }
27+
28+ private void testWrite () {
29+
30+ ByteBuffer byteBudder = ByteBuffer .allocateDirect (500 );
31+ for (int i = 0 ; i < 100000 ; i ++) {
32+ for (int j = 0 ; j < 99 ; j ++) {
33+ byteBudder .putInt (j );
34+ }
35+ byteBudder .flip ();
36+ for (int j = 0 ; j < 99 ; j ++) {
37+ byteBudder .get ();
38+ }
39+ byteBudder .clear ();
40+ }
41+ long endTime = System .currentTimeMillis ();
42+
43+ }
44+
45+ public static void main (String [] args ) {
46+ AccessDirectBuffer accessDirectBuffer = new AccessDirectBuffer ();
47+ accessDirectBuffer .bufferAccess ();;
48+ accessDirectBuffer .directAccess ();
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ package com .sun .jvm .Jvm ;
2+
3+ import java .util .Vector ;
4+
5+ /**
6+ * @ClassName DumpOOM
7+ * @Despacito TODO
8+ * @Author chenzhuo
9+ * @Version 1.0
10+ **/
11+ public class DumpOOM {
12+ public static void main (String [] args ) {
13+ Vector <byte []> vector = new Vector <>();
14+ for (int i =0 ;i <25 ;i ++){
15+ vector .add (new byte [1024 *1024 ]);
16+ }
17+
18+ }
19+ }
Original file line number Diff line number Diff line change 88 * -Xmx20m -Xms5m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC
99 **/
1010public class SimpleHeadpAlloc {
11- public static void main (String [] args ) {
11+ public static void main (String [] args ) throws InterruptedException {
1212 getOut ();
13- byte [] b = new byte [1 * 1024 * 1024 ];
13+ byte [] b = new byte [1024 * 1024 ];
1414 System .out .println ("分配了1m空间给数组" );
1515 getOut ();
1616 b = new byte [4 *1024 *1024 ];
1717 System .out .println ("分配了4m空间给数组" );
1818 getOut ();
19+ Thread .sleep (30000 );
1920 }
2021
2122 private static void getOut () {
You can’t perform that action at this time.
0 commit comments