麻烦大家帮我看看这个内存溢出怎么解决
public class ByteList {
public static final int INITIAL_SIZE = 1024;//32768;
private byte buffer[] = new byte[INITIAL_SIZE];
private int index = 0;
public byte[]detach()
{
byte result[] = new byte[index];
System.arraycopy(buffer,0,result,0,index);
buffer = null;
index = 0;
return result;
}
private int capacity()
{
return( buffer.length-index );
}
private void expand()
{
byte newbuffer[] = new byte[buffer.length+INITIAL_SIZE];
System.arraycopy(buffer,0,newbuffer,0,index);
//buffer = null;
//System.gc();
buffer = newbuffer;
}
public void append(byte buffer[])
{
System.arraycopy(buffer,0,this.buffer,index,buffer.length);
}
//max:实际要采集的文件大小
public void read(InputStream in,int max)
throws IOException
{
long l = 0;
do {
int size;
// only read up to the max size, if the max size was
// specified
if ( max!=-1 )
size = Math.min(capacity(),max); //每次最多读取32K大小的数据块
else
size = capacity();
// actually read the block
//l = in.read(buffer,index,capacity());
l = in.read(buffer,index,size);
// quit if we hit EOF
if ( l<0 )
break;
// adjust capacity if needed
index+=l;
if ( capacity()<10 )
expand();
// see if we hit the max length
if ( max!=-1 ) {
max -= l;
if ( max<=0 )
break;
}
} while ( l!=0 );
}
}
程序的功能是实现对网络视频音频的抓取,这只是其中的一个抓取模块。很有意思的是,最开始我设置INITIAL_SIZE = 1000000,只能抓取图片,而后我将其改为INITIAL_SIZE = 1024,
就可以抓取视频和音频,但是视频只能抓取五六十M的,上百M的就会报错“java.lang.OutOfMemoryError: Java heap space”,但是这一情况也不稳定,再多抓取几次过后,设置为1024
的这样的情况同样报内存溢出的错,谁若有这方面的经验,请告知,谢谢! 我觉得是不是在这个模块的读操作上出了问题。
”