场景描述
我有一个400M的二进制文件abc.bat,需要读取文件内容进行解析。功能开发完成后验证时,一共试了4台安装了DGS加密软件的办公电脑和3台个人笔记本电脑,发现在其中有2台安装了DGS加密软件的电脑上,出现了内存消耗及其不正常的情况,其他2台办公电脑和3个个人本都没有问题,所以也不知道是不是加密软件导致(因为最近2个月中加密软件给我们开发工作带来了各种诡异问题,所以个人更倾向于是加密软件导致)。
在有问题的2台电脑上内存消耗情况如下:
方式1----RandomAccessFile,消耗28G(没错,是28个G!!!)
方式2----FileInputStream,消耗11G(没错,是11个G!!!)
方式3-—BufferedInputStream,消耗800M。
方式1:
File localFile = new File("D:/abc.dat");
byte[] bytes = new byte[1024];
try(RandomAccessFile randomAccessFile = new RandomAccessFile(localFile, "rw")) {
while (randomAccessFile.read(bytes) > -1) {
}
} catch (Exception e) {
}
方式2:
File localFile = new File("D:/abc.dat");
byte[] bytes = new byte[1024];
try(FileInputStream fileInputStream = new FileInputStream(localFile)) {
while (fileInputStream.read(bytes) > -1) {
}
} catch (Exception e) {
}
方式3:
File localFile = new File("D:/abc.dat");
byte[] bytes = new byte[1024];
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(localFile))) {
while (bufferedInputStream.read(bytes) > -1) {
}
} catch (Exception e) {
}
虽然使用RandomAccessFile、FileInputStream、BufferedInputStream不同的3种方式,但是我是读取的相同的文件、且都是通过一个1024字节大小的数组来循环读取内容的,并没有把全部文件读入到内存,为什么会出现RandomAccessFile、FileInputStream在个别的电脑上内存消耗不正常的情况呢?因为什么导致的呢?