zoukankan      html  css  js  c++  java
  • 垃圾回收

     1 public class TestNIO {
     2     /**
     3      * 测试堆内存缓冲区
     4      */
     5     @Test
     6     public void test1HeapByteBuffer(){
     7         int size = 1024 * 1024 * 450 ;
     8         ByteBuffer buf = ByteBuffer.allocate(size);
     9         System.out.println(buf.capacity());
    10         buf = null ;
    11         System.gc();        //OK
    12         System.out.println("kkk");
    13     }
    14     
    15     /**
    16      * 测试堆内存缓冲区
    17      * @throws Exception 
    18      */
    19     @Test
    20     public void test1DirectByteBuffer() throws Exception{
    21         
    22         int size = 1024 * 1024 * 1024 * 1 ;
    23         ByteBuffer buf = ByteBuffer.allocateDirect(size);
    24         
    25         //通过反射,回收离堆内存
    26         Class clazz = Class.forName("java.nio.DirectByteBuffer");
    27         Method m = clazz.getDeclaredMethod("cleaner");
    28         m.setAccessible(true);
    29         Object cleaner = m.invoke(buf);
    30         
    31         Class cleanerClazz = cleaner.getClass();
    32         Method m2 = cleanerClazz.getDeclaredMethod("clean");
    33         m2.setAccessible(true);
    34         m2.invoke(cleaner);
    35         
    36         System.out.println(buf.get(1000));
    37         System.gc();
    38         System.out.println("dd");
    39     }
    40     /**
    41      * 测试垃圾回收
    42      */
    43     @Test
    44     public void testGC(){
    45         int size = Integer.MAX_VALUE;
    46         byte[] buf = new byte[size];
    47         byte[] buf2 = buf ;
    48         List<byte[]> list = new ArrayList<byte[]>();
    49         list.add(buf2);
    50         buf = null ;
    51         System.gc();    //进行垃圾回收
    52         buf2 = null ;
    53         System.gc();    //进行垃圾回收
    54         System.out.println("xxx");
    55         list.clear();    //清空集合
    56         System.gc();    //进行垃圾回收
    57         System.out.println("xxx");
    58     }
    59     
    60     /**
    61      * 测试垃圾回收
    62      */
    63     @Test
    64     public void testChannel() throws Exception{
    65         FileInputStream fis = new FileInputStream("d:/a.txt");
    66         FileChannel srcFC = fis.getChannel();
    67         
    68         FileOutputStream fos = new FileOutputStream("d:/b.txt");
    69         FileChannel destFC = fos.getChannel();
    70         
    71         //定义缓冲区
    72         ByteBuffer buf = ByteBuffer.allocate(11);
    73         while(srcFC.read(buf) != -1){
    74             buf.flip();
    75             destFC.write(buf);
    76             buf.clear();
    77         }
    78         fis.close();
    79         fos.close();
    80     }
    81 }
  • 相关阅读:
    sql查询原理和Select执行顺序(sql优化)
    SQL Server如何清除连接过的服务器名称历史?
    SQL Server DATEDIFF()
    oracle 创建用户、表空间
    IDEA 快捷键
    (Reflection)反射
    ClassLoader
    简单代码// 求质数
    简单代码 //输入三个数求最大值.
    简单代码 //输入两个数加减乘除。
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6785682.html
Copyright © 2011-2022 走看看