麻烦大家帮我看看这个内存溢出怎么解决
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
的这样的情况同样报内存溢出的错,谁若有这方面的经验,请告知,谢谢! 我觉得是不是在这个模块的读操作上出了问题。
”
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
的这样的情况同样报内存溢出的错,谁若有这方面的经验,请告知,谢谢! 我觉得是不是在这个模块的读操作上出了问题。
”
...全文
相关推荐
maven内存溢出解决放啊
maven构建时发生内存溢出的有很多种情况,比如eclipse-plugin,maven命令时,这里总结了分别怎么解决。
图片过大引起的内存溢出解决方案
使用BitmapFactory加载图片时,如果图片过大可能引起OutOfmemory溢出的问题。解决办法。
Tomcat内存溢出三种情况及解决办法
Tomcat内存溢出三种情况及解决办法,不同的异常采用不同的方式解决,不能盲目的增加内存就可以了。
完美解决java读取大文件内存溢出的问题
下面小编就为大家带来一篇完美解决java读取大文件内存溢出的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
解决大批量数据导出Excel产生内存溢出的方案
解决大批量数据导出Excel产生内存溢出的方案


Java SE
