java程序如何将8g的文件,在jvm内存2g的情况下


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test {
    public static void main(String[] args) throws Exception{
        Long startTime = System.currentTimeMillis();

        testByteBuffer();

        Long endTime = System.currentTimeMillis();
        // 计算并打印耗时
        Long tempTime = (endTime - startTime);
        System.out.println("花费时间:"+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));

    }

    static File file = new File("E:\\Linux【更多IT资源加微信wxdhhfy】\\Linux\\hadoop000-Snapshot1.vmem");

    static  int bufferSize = 1024;

    public static void testByteBuffer() throws IOException {
        FileChannel channel = new FileInputStream(file).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
        int bytes;
        while ((bytes = channel.read(buffer)) != -1) {
            byte[] data = new byte[bytes];
            buffer.flip();
            buffer.get(data);
            buffer.clear();
        }
        channel.close();
    }
}

在这里插入图片描述
控制变量,通过设置VM options参数-Xmx
-Xmx 为JVM运行时可申请的最大Heap值,默认值为物理内存的1/4但小于1G,默认当空余堆内存小于40%时,JVM会增大Heap到-Xmx指定的大小,可通过-XX:MinHeapFreeRation来指定这个比列。
在这里插入图片描述
同事的解决方法是使用BufferedReader,因为其中同样有缓冲区,还有readLine方法
在这里插入图片描述
解析大文件,并存到mq中,在同一个服务中使用多线程进行消费,存到es中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值