内存管理策略1

package com.prime.impl;

import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * <p>
 * 该内存管理对象主要是在当你需要长时间的new一快内存的时候使用,
 * <p>
 * <b>(主要作用是为了不让GC对这些内存不停的释放分配而消耗性能,而且每次获取的内存大小可以是自己指定的大小)</b>
 * <p>
 * 本内存管理对象主要通过预先分配一个大的ByteBuffer然后每次从这个ByteBuffer中获取一小块内存出来进行使用,
 * <p>
 * 具体获取内存的规则是在打的内存中获取一段连续的内存出来使用,如果中间有一小段内存(例如三个字节)未可以使用,
 * <p>
 * 但如果你获取的内存都币这三内存大的话,则永远获取不到该内存(这里就会产生一小块的内存碎片),
 * <p>
 * 当然只要该小段内存的前后被释放后将又可以获取使用
 * <p>
 * 主要是通过ByteBuffer的子序列来做到的,如果当预先分配的内存不足的时候,将重新分配另快大的内存
 * <p>
 * (该该重新分配的内存也是有初始化的时候用使用者设置,重新分配的内存将由本内存管理对象中的子内存管理对象所拥有,
 * <p>
 * 主要算法是通过链表的形式来实现,理论上当总内存不可用的时候可以无限分配新的内存) <br/>
 * 
 * @author 皮佳
 * 
 */
public class MomoryManagerByte implements MemoryObjInface {

	private static int defaultSize = 1024;
	/**
	 * Create size
	 */
	private static int byte_size = 1024 * 1024 * 1;

	private static int dilatancy_size = 1024 * 1024 * 1;

	/**
	 * 是否VM托管
	 */
	private static boolean isDirect = false;

	private ByteBuffer byteBuffer = null;

	private TreeSet<MomoryBuffer> bufferSet = new TreeSet<MomoryBuffer>(
			new MomoryBufferCommpositor());
	private MomoryManagerByte momoryManagerByte = null;

	public MomoryManagerByte() {
		this(byte_size, defaultSize, dilatancy_size, isDirect);
	}

	public MomoryManagerByte(boolean isDirect) {
		this(byte_size, defaultSize, dilatancy_size, isDirect);
	}

	public MomoryManagerByte(int byteSize) {
		this(byteSize, defaultSize, dilatancy_size, isDirect);
	}

	public MomoryManagerByte(int byteSize, boolean isDirect) {
		this(byteSize, defaultSize, dilatancy_size, isDirect);
	}

	public MomoryManagerByte(int byteSize, int defaultSize, int dilatancySize,
			boolean isDirect) {
		this.byte_size = byteSize;
		this.defaultSize = defaultSize;
		this.dilatancy_size = dilatancySize;
		this.isDirect = isDirect;

		if (this.isDirect) {
			byteBuffer = ByteBuffer.allocateDirect(this.byte_size);
		} else {
			byteBuffer = ByteBuffer.allocate(this.byte_size);
		}
	}

	@Override
	public ByteBuffer allocat() {
		return this.allocat(this.defaultSize);
	}

	@Override
	public ByteBuffer allocat(int size) {
	//先从总内存中获取
		ByteBuffer byteBuffer =gain(size);
		return null;
	}
	/**
	 * 从byteBuffer中获取一块内存出来使用
	 * @param size
	 * @return
	 */
	private ByteBuffer gain(int size) {
		boolean bor = false;
		//如果还没有获取过内存就直接从第一个位置开始获取
		if(bufferSet == null || bufferSet.size()<=0){
			this.byteBuffer.position(0);
			this.byteBuffer.limit(size);
			bor = true;
		}
		else{
			//如果之前获取过 
			synchronized (this.bufferSet) {
				//遍历之前获取的内存对象 拿到它的索引值 根据索引值来接着后面的位置获取
				Iterator<MomoryBuffer> iter =  bufferSet.iterator();
				int position = 0;
				while(iter.hasNext()){
					MomoryBuffer momoryBuffer = iter.next();
					if((momoryBuffer.getPosition() - position) >= size){
						this.byteBuffer.position(position);
						this.byteBuffer.limit(momoryBuffer.getPosition());
						bor = true;
						break;
					}
					position = momoryBuffer.getLimit();
				}
				if((this.byte_size - position) >= size){
					this.byteBuffer.position(position);
					this.byteBuffer.limit(position + size);
					bor = true;
				}
			}
		}
		ByteBuffer slicebuf = null;
		if(bor){
			slicebuf = this.byteBuffer.slice();
//			this.getBufferSet().add(new MomoryBuffer(slicebuf,slicebuf.arrayOffset(),slicebuf.arrayOffset() + slicebuf.limit()));
			this.getBufferSet().add(new MomoryBuffer(slicebuf,this.byteBuffer.position(),this.byteBuffer.limit()));
		}
		this.byteBuffer.clear();
		return slicebuf;
	}
	private ByteBuffer getByteBuffer() {
		return byteBuffer;
	}

	/**
	 * 返回正在使用的ByteBuffer队列,用来标示有哪些区间已经在使用了
	 * 
	 * @return
	 */
	private TreeSet<MomoryBuffer> getBufferSet() {
		return bufferSet;
	}
	@Override
	public void free(ByteBuffer buf) throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 用来封装去出来的内存,主要是为了表示该内存用到了总内存中的哪些区间
	 * 
	 * @author cloud _
	 */
	class MomoryBuffer {
		private ByteBuffer buf = null;
		private int position = 0;
		private int limit = 0;

		public MomoryBuffer(ByteBuffer _buf, int _position, int _limit) {
			this.buf = _buf;
			this.position = _position;
			this.limit = _limit;
		}

		public ByteBuffer getBuf() {
			return buf;
		}

		public int getPosition() {
			return position;
		}

		public int getLimit() {
			return limit;
		}
	}

	/**
	 * 一个排序器,用来将MomoryBuffer进行排序
	 * 
	 * @author cloud
	 * 
	 */
	class MomoryBufferCommpositor implements Comparator<MomoryBuffer> {

		public int compare(MomoryBuffer o1, MomoryBuffer o2) {
			int position_1 = o1.getPosition();
			int position_2 = o2.getPosition();

			if (position_1 > position_2) {
				return 1;
			}
			if (position_1 < position_2) {
				return -1;
			}
			return 0;
		}
	}
}

你可能感兴趣的:(算法)