java 多种文件复制方式(1g文件和4g文件)

java8,测试1g文件copy和4g文件拷贝时效率

vm参数设置: -XX:+PrintGCDetails -Xmx1G -Xms1G

源码

​
import java.io.*;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 4g文件,copy测试
 */
public class FileCopyTest extends AbstractTest {
    private String source = "D:\\test\\bigfile.txt";

    @Override
    protected void head() {
        try {
            //创建文件,1g(1L<<30) 4g(1L<<32)
            create(source, 1L << 30);
            //文件复制方式第一种,stream直接buffer,1g->5s 4g->33s
            streamCopy(source, "D:\\test\\streamCopy.txt");
            //文件复制方式Files.copy,用户态文件copy,1g->11s 4g->40s
            toolCopy(source, "D:\\test\\toolCopy.txt");
            //文件复制方式Channel复制,1g->3s 4g->38s
            channelCopy(source, "D:\\test\\channelCopy.txt");
            //文件复制方式MappedByteBuffer复制,1g->6s 4g->42s
            mappedByteBufferCopy(source, "D:\\test\\mappedByteBufferCopy.txt");
            //文件复制通道RandomAccessFile,1g->3s 4g->38s
            channelRandomCopy(source, "D:\\test\\channelRandomCopy.txt");
            //多线程复制,跟gc和线程切换有关,1g->5s ,4g->40-60
            multiThreadCopy(source, "D:\\test\\multiThreadCopy.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void create(String target, long size) throws IOException {
        Path path = Paths.get(target);
        if (Files.isDirectory(path)) {
            throw new IOException("不是有效的文件" + target);
        }
        if (Files.exists(path)) {
            Files.delete(path);
            Files.createFile(path);
        }

        //512m
        long stepLength = 1L << 29;
        long cur = 0L;
        MappedByteBuffer mappedByteBuffer;
        Random random = new Random();
        while (cur < size) {
            mappedByteBuffer = new RandomAccessFile(target, "rw").getChannel()
                    .map(FileChannel.MapMode.READ_WRITE, cur, stepLength);
            IntBuffer intBuffer = mappedByteBuffer.asIntBuffer();
            while (intBuffer.position() < intBuffer.capacity()) {
                intBuffer.put(random.nextInt());
            }
            cur += stepLength;
        }
    }

    private void multiThreadCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        RandomAccessFile in = new RandomAccessFile(source, "r");
        RandomAccessFile out = new RandomAccessFile(target, "rw");

        ExecutorService executorService = Executors.newFixedThreadPool(4);
        long size = in.length();
        //512m
        long stepLength = 1L << 29;
        int partition = (int) (size / stepLength);
        System.out.println("分块:" + partition);
        System.out.println("步长:" + FileUtils.size(stepLength));
        CountDownLatch countDownLatch = new CountDownLatch(partition);
        FileChannel fileChannel = in.getChannel();
        for (int i = 0; i < partition; i++) {
            long cur = i * stepLength;
            executorService.execute(new CopyTask(fileChannel, cur, stepLength, out, countDownLatch));
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
        long end = System.currentTimeMillis();
        System.out.println("multiThreadCopy耗时:" + (end - start) / 1000L);
    }

    private static class CopyTask implements Runnable {
        private long cur;
        private RandomAccessFile out;
        private CountDownLatch countDownLatch;
        private FileChannel fileChannel;
        private long stepLength;

        public CopyTask(FileChannel fileChannel, long cur, long stepLength, RandomAccessFile out, CountDownLatch countDownLatch) {
            this.cur = cur;
            this.out = out;
            this.countDownLatch = countDownLatch;
            this.stepLength = stepLength;
            this.fileChannel = fileChannel;
        }

        @Override
        public void run() {
            try {
                MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, cur, stepLength);
                out.getChannel().write(mappedByteBuffer, cur);
            } catch (IOException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }
    }

    /**
     * mappedByteBuffer,涉及到GC,空间没有重复使用
     *
     * @throws IOException
     */
    private void mappedByteBufferCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        RandomAccessFile in = new RandomAccessFile(source, "r");
        RandomAccessFile out = new RandomAccessFile(target, "rw");
        MappedByteBuffer inMappedByteBuffer;

        long cur = 0L;
        long stepLength = 1L << 29;
        while (cur < in.length()) {
            inMappedByteBuffer =
                    in.getChannel().map(FileChannel.MapMode.READ_ONLY, cur, stepLength);
            out.getChannel().write(inMappedByteBuffer, cur);
            cur += stepLength;
        }
        long end = System.currentTimeMillis();
        System.out.println("channelCopy耗时:" + (end - start) / 1000L);
    }

    /**
     * 理论上是最快的
     *
     * @throws IOException
     */
    private void channelCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        File in = new File(source);
        FileInputStream fileInputStream = new FileInputStream(in);
        File out = new File(target);
        if (!out.exists()) {
            out.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(out);
        long fileLength = in.length();
        long cur = 0L;
        //1G
        long stepLength = 1L << 30;
        while (cur < fileLength) {
            fileInputStream.getChannel().transferTo(cur, stepLength, fileOutputStream.getChannel());
            cur += stepLength;
        }
        fileInputStream.close();
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("channelCopy耗时:" + (end - start) / 1000L);
    }

    /**
     * 和直接使用file.getChannel应该是一样的
     *
     * @throws IOException
     */
    private void channelRandomCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        RandomAccessFile in = new RandomAccessFile(source, "r");
        RandomAccessFile out = new RandomAccessFile(target, "rw");
        long fileLength = in.length();
        long cur = 0L;
        long stepLength = 1L << 30;
        while (cur < fileLength) {
            in.getChannel().transferTo(cur, stepLength, out.getChannel());
            cur += stepLength;
        }
        in.close();
        out.close();
        long end = System.currentTimeMillis();
        System.out.println("channelRandomCopy耗时:" + (end - start) / 1000L);
    }

    /**
     * 直接调用底层C++代码复制,用户态-内核态切换
     *
     * @throws IOException
     */
    private void toolCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        File out = new File(target);
        FileOutputStream fileOutputStream = new FileOutputStream(out);
        Files.copy(Paths.get(source), fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("toolCopy耗时:" + (end - start) / 1000L);
    }

    /**
     * 分析,重复使用buffer,没有gc
     *
     * @throws IOException
     */
    private void streamCopy(String source, String target) throws IOException {
        long start = System.currentTimeMillis();
        File in = new File(source);
        FileInputStream fileInputStream = new FileInputStream(in);

        File out = new File(target);
        if (!out.exists()) {
            out.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(out);
        //1M
        byte[] buffer = new byte[1 << 20];
        int length;
        while ((length = fileInputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        fileOutputStream.flush();
        fileOutputStream.close();
        fileInputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("streamCopy耗时:" + (end - start) / 1000L);
    }

    private static class FileUtils {
        private static final long B = 1024L;
        private static final long K = 1048576L;
        private static final long M = 1073741824L;
        private static final long G = 1099511627776L;
        private static final long T = 1125899906842624L;
        private static final long P = 1152921504606846976L;

        /**
         * 单位换算,(这里如果想用stringbuilder来appen也是可以的
         * ,如果您javap 命令反编译出class文件,会发现其实底层优化,自动使用了stringbuilder)
         *
         * @param size
         * @return
         */
        public static String size(long size) {
            DecimalFormat df = new DecimalFormat("#.00");
            String fileSizeString = "";
            String kSize = df.format((double) size / B) + "K";
            if (size < B) {
                fileSizeString = kSize + "(" + df.format((double) size) + "B" + ")";
            } else if (size < K) {
                fileSizeString = df.format((double) size / B) + "K";
            } else if (size < M) {
                fileSizeString = kSize + "(" + df.format((double) size / K) + "M" + ")";
            } else if (size < G) {
                fileSizeString = kSize + "(" + df.format((double) size / M) + "G" + ")";
            } else if (size < T) {
                fileSizeString = kSize + "(" + df.format((double) size / G) + "T)";
            } else if (size < P) {
                fileSizeString = kSize + "(" + df.format((double) size / T) + "P)";
            } else {
                fileSizeString = kSize + "(" + df.format((double) size / P) + "E)";
            }
            return fileSizeString;
        }
    }
}

​

1.InputStream和OutputStream

1G文件

streamCopy耗时:4
Heap
 def new generation   total 314560K, used 22398K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   8% used [0x04800000, 0x05ddfbc0, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 0K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19d50000, 0x19d50200, 0x44800000)
 Metaspace       used 3984K, capacity 4024K, committed 4032K, reserved 4480K

4G文件

streamCopy耗时:34
Heap
 def new generation   total 314560K, used 22398K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   8% used [0x04800000, 0x05ddfbc0, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 0K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19d50000, 0x19d50200, 0x44800000)
 Metaspace       used 3984K, capacity 4024K, committed 4032K, reserved 4480K

2.Files.copy()

1G文件

toolCopy耗时:5
Heap
 def new generation   total 314560K, used 22398K [0x04e00000, 0x1a350000, 0x1a350000)
  eden space 279616K,   8% used [0x04e00000, 0x063dfbc0, 0x15f10000)
  from space 34944K,   0% used [0x15f10000, 0x15f10000, 0x18130000)
  to   space 34944K,   0% used [0x18130000, 0x18130000, 0x1a350000)
 tenured generation   total 699072K, used 0K [0x1a350000, 0x44e00000, 0x44e00000)
   the space 699072K,   0% used [0x1a350000, 0x1a350000, 0x1a350200, 0x44e00000)
 Metaspace       used 4198K, capacity 4248K, committed 4288K, reserved 4480K

4G文件

toolCopy耗时:41
Heap
 def new generation   total 314560K, used 22398K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   8% used [0x04800000, 0x05ddfbc0, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 0K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19d50000, 0x19d50200, 0x44800000)
 Metaspace       used 4198K, capacity 4248K, committed 4288K, reserved 4480K

3.FileChannel.transferTo()

1G文件

channelCopy耗时:3
Heap
 def new generation   total 314560K, used 22398K [0x04a00000, 0x19f50000, 0x19f50000)
  eden space 279616K,   8% used [0x04a00000, 0x05fdfbc0, 0x15b10000)
  from space 34944K,   0% used [0x15b10000, 0x15b10000, 0x17d30000)
  to   space 34944K,   0% used [0x17d30000, 0x17d30000, 0x19f50000)
 tenured generation   total 699072K, used 0K [0x19f50000, 0x44a00000, 0x44a00000)
   the space 699072K,   0% used [0x19f50000, 0x19f50000, 0x19f50200, 0x44a00000)
 Metaspace       used 4065K, capacity 4123K, committed 4160K, reserved 4480K

4G文件

channelCopy耗时:37
Heap
 def new generation   total 314560K, used 22398K [0x04a00000, 0x19f50000, 0x19f50000)
  eden space 279616K,   8% used [0x04a00000, 0x05fdfbc0, 0x15b10000)
  from space 34944K,   0% used [0x15b10000, 0x15b10000, 0x17d30000)
  to   space 34944K,   0% used [0x17d30000, 0x17d30000, 0x19f50000)
 tenured generation   total 699072K, used 0K [0x19f50000, 0x44a00000, 0x44a00000)
   the space 699072K,   0% used [0x19f50000, 0x19f50000, 0x19f50200, 0x44a00000)
 Metaspace       used 4067K, capacity 4123K, committed 4160K, reserved 4480K

4.RandomAccessFile

1G文件

channelRandomCopy耗时:3
Heap
 def new generation   total 314560K, used 22398K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   8% used [0x04800000, 0x05ddfbc0, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 0K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19d50000, 0x19d50200, 0x44800000)
 Metaspace       used 4077K, capacity 4123K, committed 4160K, reserved 4480K

4G文件

channelRandomCopy耗时:38
Heap
 def new generation   total 314560K, used 22398K [0x04a00000, 0x19f50000, 0x19f50000)
  eden space 279616K,   8% used [0x04a00000, 0x05fdfbc0, 0x15b10000)
  from space 34944K,   0% used [0x15b10000, 0x15b10000, 0x17d30000)
  to   space 34944K,   0% used [0x17d30000, 0x17d30000, 0x19f50000)
 tenured generation   total 699072K, used 0K [0x19f50000, 0x44a00000, 0x44a00000)
   the space 699072K,   0% used [0x19f50000, 0x19f50000, 0x19f50200, 0x44a00000)
 Metaspace       used 4077K, capacity 4123K, committed 4160K, reserved 4480K

5.MappedByteBuffer

1G文件

[Full GC (System.gc()) [Tenured: 0K->1823K(699072K), 0.0141744 secs] 22398K->1823K(1013632K), [Metaspace: 4065K->4065K(4480K)], 0.0169870 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
channelCopy耗时:6
Heap
 def new generation   total 314560K, used 11184K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   4% used [0x04800000, 0x052ec320, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 1823K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19f17db0, 0x19f17e00, 0x44800000)
 Metaspace       used 4072K, capacity 4120K, committed 4160K, reserved 4480K

4G文件

[Full GC (System.gc()) [Tenured: 0K->1823K(699072K), 0.0281413 secs] 22398K->1823K(1013632K), [Metaspace: 4065K->4065K(4480K)], 0.0311212 secs] [Times: user=0.01 sys=0.02, real=0.03 secs] 
[Full GC (System.gc()) [Tenured: 1823K->1824K(699072K), 0.0048135 secs] 13008K->1824K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0048608 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1824K->1824K(699072K), 0.0093538 secs] 13008K->1824K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0094431 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1824K->1163K(699072K), 0.0075926 secs] 7417K->1163K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0076662 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1163K->1163K(699072K), 0.0115011 secs] 6755K->1163K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0116041 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1163K->1164K(699072K), 0.0082543 secs] 6756K->1164K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0083320 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1164K->1165K(699072K), 0.0129058 secs] 6756K->1165K(1013632K), [Metaspace: 4066K->4066K(4480K)], 0.0130133 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
channelCopy耗时:42
Heap
 def new generation   total 314560K, used 5592K [0x04800000, 0x19d50000, 0x19d50000)
  eden space 279616K,   2% used [0x04800000, 0x04d761a8, 0x15910000)
  from space 34944K,   0% used [0x15910000, 0x15910000, 0x17b30000)
  to   space 34944K,   0% used [0x17b30000, 0x17b30000, 0x19d50000)
 tenured generation   total 699072K, used 1165K [0x19d50000, 0x44800000, 0x44800000)
   the space 699072K,   0% used [0x19d50000, 0x19e734c8, 0x19e73600, 0x44800000)
 Metaspace       used 4073K, capacity 4120K, committed 4160K, reserved 4480K

6.多线程复制

1G文件

分块:8
步长:131072.00K(128.00M)
[Full GC (System.gc()) [Tenured: 0K->1870K(699072K), 0.0112641 secs] 44768K->1870K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0113534 secs] [Times: user=0.01 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1870K->1871K(699072K), 0.0070772 secs] 24240K->1871K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0071387 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
multiThreadCopy耗时:5
Heap
 def new generation   total 314560K, used 33554K [0x04a00000, 0x19f50000, 0x19f50000)
  eden space 279616K,  12% used [0x04a00000, 0x06ac4960, 0x15b10000)
  from space 34944K,   0% used [0x15b10000, 0x15b10000, 0x17d30000)
  to   space 34944K,   0% used [0x17d30000, 0x17d30000, 0x19f50000)
 tenured generation   total 699072K, used 1871K [0x19f50000, 0x44a00000, 0x44a00000)
   the space 699072K,   0% used [0x19f50000, 0x1a123ed8, 0x1a124000, 0x44a00000)
 Metaspace       used 4155K, capacity 4184K, committed 4288K, reserved 4480K

4G文件

分块:32
步长:131072.00K(128.00M)
[Full GC (System.gc()) [Tenured: 0K->1872K(699072K), 0.0186461 secs] 44768K->1872K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0187987 secs] [Times: user=0.02 sys=0.01, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1872K->1872K(699072K), 0.0131360 secs] 18649K->1872K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0132463 secs] [Times: user=0.02 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1872K->1873K(699072K), 0.0070749 secs] 18649K->1873K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0071370 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1873K->1211K(699072K), 0.0064547 secs] 13058K->1211K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0065091 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1211K(699072K), 0.0067938 secs] 12395K->1211K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0068554 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0046318 secs] 12396K->1212K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0046733 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1213K(699072K), 0.0050816 secs] 12397K->1213K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0051401 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1210K(699072K), 0.0123529 secs] 12398K->1210K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0124787 secs] [Times: user=0.01 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1210K->1211K(699072K), 0.0121111 secs] 12395K->1211K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0122155 secs] [Times: user=0.02 sys=0.01, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0119589 secs] 12396K->1212K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0120655 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1213K(699072K), 0.0050013 secs] 12397K->1213K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0050459 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1210K(699072K), 0.0096042 secs] 12397K->1210K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0096898 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1210K->1211K(699072K), 0.0127724 secs] 12395K->1211K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0128951 secs] [Times: user=0.02 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0069125 secs] 12396K->1212K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0069763 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1212K(699072K), 0.0123873 secs] 12396K->1212K(1013632K), [Metaspace: 4146K->4146K(4480K)], 0.0124939 secs] [Times: user=0.02 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1211K(699072K), 0.0125100 secs] 17989K->1211K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0126220 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0099254 secs] 12396K->1212K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0100129 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1213K(699072K), 0.0090147 secs] 12397K->1213K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0090928 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1213K(699072K), 0.0066715 secs] 12397K->1213K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0067331 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1211K(699072K), 0.0118469 secs] 12398K->1211K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0119464 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0094466 secs] 12396K->1212K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0095368 secs] [Times: user=0.01 sys=0.02, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1212K(699072K), 0.0118130 secs] 12397K->1212K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0119286 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1213K(699072K), 0.0082931 secs] 12397K->1213K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0083596 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1211K(699072K), 0.0130539 secs] 12398K->1211K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0131914 secs] [Times: user=0.01 sys=0.02, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1211K->1212K(699072K), 0.0133806 secs] 12396K->1212K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0135109 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1212K(699072K), 0.0132605 secs] 12396K->1212K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0134015 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1212K->1213K(699072K), 0.0123650 secs] 12397K->1213K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0124783 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1213K->1211K(699072K), 0.0150080 secs] 12398K->1211K(1013632K), [Metaspace: 4149K->4149K(4480K)], 0.0151485 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
multiThreadCopy耗时:56
Heap
 def new generation   total 314560K, used 27961K [0x05200000, 0x1a750000, 0x1a750000)
  eden space 279616K,  10% used [0x05200000, 0x06d4e7e8, 0x16310000)
  from space 34944K,   0% used [0x16310000, 0x16310000, 0x18530000)
  to   space 34944K,   0% used [0x18530000, 0x18530000, 0x1a750000)
 tenured generation   total 699072K, used 1211K [0x1a750000, 0x45200000, 0x45200000)
   the space 699072K,   0% used [0x1a750000, 0x1a87ec90, 0x1a87ee00, 0x45200000)
 Metaspace       used 4157K, capacity 4187K, committed 4288K, reserved 4480K

Ohter.创建4g文件

[Full GC (System.gc()) [Tenured: 0K->1837K(699072K), 0.0096224 secs] 22398K->1837K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0096961 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (System.gc()) [Tenured: 1837K->1838K(699072K), 0.0053253 secs] 13022K->1838K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0053815 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1838K->1839K(699072K), 0.0088826 secs] 13023K->1839K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0089286 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1839K->1177K(699072K), 0.0044623 secs] 7431K->1177K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0044984 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1177K->1178K(699072K), 0.0067081 secs] 6769K->1178K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0067496 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (System.gc()) [Tenured: 1178K->1179K(699072K), 0.0045560 secs] 6770K->1179K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0046011 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [Tenured: 1179K->1180K(699072K), 0.0039339 secs] 6771K->1180K(1013632K), [Metaspace: 4253K->4253K(4480K)], 0.0039759 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 314560K, used 5592K [0x05400000, 0x1a950000, 0x1a950000)
  eden space 279616K,   2% used [0x05400000, 0x059761a8, 0x16510000)
  from space 34944K,   0% used [0x16510000, 0x16510000, 0x18730000)
  to   space 34944K,   0% used [0x18730000, 0x18730000, 0x1a950000)
 tenured generation   total 699072K, used 1180K [0x1a950000, 0x45400000, 0x45400000)
   the space 699072K,   0% used [0x1a950000, 0x1aa77090, 0x1aa77200, 0x45400000)
 Metaspace       used 4260K, capacity 4312K, committed 4416K, reserved 4480K

结论:

1.FileChannel.transferTo()在小于2G时,效率最高,跟直接内核态拷贝(零拷贝)的机制有关。

2.系统直接fullgc,是因为直接new的大数组对象做buffer

3.buffer重复利用,避免触发GC,消除stop the world 停顿时间

在Java9 以及高版本中使用xlog参数打印jvm日志。

猜你喜欢

转载自blog.csdn.net/bpz31456/article/details/82286694
今日推荐