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
的这样的情况同样报内存溢出的错,谁若有这方面的经验,请告知,谢谢! 我觉得是不是在这个模块的读操作上出了问题。
”
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
的这样的情况同样报内存溢出的错,谁若有这方面的经验,请告知,谢谢! 我觉得是不是在这个模块的读操作上出了问题。
”
JAVA_OPTS='-Xms512m -Xmx1024m'
我以前自己碰到的情况是突然读入大量数据时候,虚拟机来不及释放,system.gc()没用,或者虚拟机设置运行的那个maxmemory太小。或者本身这个程序就用了很大内存。
建议使用C++
这里可以看到加了参数后的变化
在你建议之前我就尝试了这种改动,现在的情况是一百多M的视频采集已经没问题了,但是遇到更大的还会报同样的错,我机子是2G的内存,
是否可以尝试-Xmx1024m 谢谢
JAVA_OPTS='-Xms512m -Xmx1024m'
java jvm 本身就占用很大的内存
这个和JVM所占的内存,么有太大关系。问题的关键,是程序所占JVM的内存,没有释放掉,造成的。
2楼的方法,就是将JVM的为程序准备的最大内存值,调节得更大一些。
个人觉得,将暂时不用的对象或者数据,写到硬盘,比较好。