MappedByteBuffer 與 普通的文件讀取方式內存佔用對比測試

package demo9;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

/**
 * MappedByteBuffer 與 普通的文件讀取方式內存佔用對比測試
 * @author mengfeiyang
 *
 */
public class MappedMemTest {

	@SuppressWarnings("resource")
	public static void main(String args[]) throws FileNotFoundException, IOException {
		File file = new File("E:\\img\\escClick-2016041810.log");//文件大小爲60M
		
		//List<String> ll = FileUtils.readLines(file);
		//for(String l : ll)out(l);   //此種方式內存佔用200M起
		
		
		long fileLength = file.length();
		final int BUFFER_SIZE=60;
		
		//此種方式內存佔用將穩定在20M
		MappedByteBuffer inputBuffer = new RandomAccessFile(file,"r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileLength);
		
		byte[] dst = null;
		List<Byte> dst2 = null;
		int offset = 0;
		int s = 0;
		do{
			dst = new byte[BUFFER_SIZE];
			dst2 = new ArrayList<Byte>();
			for(int a=0;a<BUFFER_SIZE;a++){
				dst[a] = inputBuffer.get(offset);
				offset ++;
			}
			
			String d1 = new String(dst,"UTF-8");
			if(!d1.endsWith("1460944800}")){
				for(int b = 0;b<2048;b++){
					byte by = inputBuffer.get(offset);
					dst2.add(by);
					offset++;
					if(by == '\n'){
						s++;
						String d2 = new String(list2Array(dst2),"UTF-8");
						System.out.println(s+"  "+d1+" --> "+d2);
						break;
					}
				}
			}
		}while(offset<fileLength);
	}
	
	public static byte[] list2Array(List<Byte> blist){
		byte[] barr = new byte[blist.size()];
		for(int i=0;i<blist.size();i++){
			barr[i] = blist.get(i);
		}
		return barr;
	}

}
相關文章
相關標籤/搜索