主题:[讨论]java outofmemory异常 增加jvm内存也没用
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class flashs extends Applet
{
URL urls;
String[] imgs;
Image[] imgi;
int locate;
Image bufferimage;
MediaTracker mt;
Graphics gpb;
public void init()
{
File f;
try
{
urls=new URL(getCodeBase(),"素材1\\");
f=new File(urls.toURI());
imgs=f.list();
imgi=new Image[imgs.length];
mt=new MediaTracker(this);
bufferimage=createImage(getWidth(),getHeight());
gpb=bufferimage.getGraphics();
gpb.setColor(new Color(255,255,255));
for(int i=0;i<imgs.length;i++)
{
imgi[i]=getImage(urls,imgs[i]);
mt.addImage(imgi[i],i);
}
try
{
if(!mt.checkID(0,true))
mt.waitForID(0);
System.out.println("第一个图片载入完毕");
gpb.fillRect(0,0,getWidth(),getHeight());
gpb.drawImage(imgi[0],-1,-1,null);
}
catch(Exception ex){System.out.println("第一个图片载入失败");}
}
catch(Exception e){}
}
public void start()
{
locate=0;
}
public void paint(Graphics g)
{
if(locate==imgi.length)
locate=0;
if(locate%9==0)
System.gc();
System.out.println(imgs[locate]);
try
{
Thread.sleep(500);
if(!mt.checkID(locate,true))
mt.waitForID(locate);
}
catch(Exception e){}
g.drawImage(bufferimage,-1,-1,null);
gpb.fillRect(0,0,getWidth(),getHeight());
gpb.drawImage(imgi[locate++],-1,-1,null);
repaint();
}
public void update(Graphics g)
{
paint(g);
}
}
//<applet code="flashs.class" Width=400 Height=500>
//</applet>
import java.awt.*;
import java.io.*;
import java.net.*;
public class flashs extends Applet
{
URL urls;
String[] imgs;
Image[] imgi;
int locate;
Image bufferimage;
MediaTracker mt;
Graphics gpb;
public void init()
{
File f;
try
{
urls=new URL(getCodeBase(),"素材1\\");
f=new File(urls.toURI());
imgs=f.list();
imgi=new Image[imgs.length];
mt=new MediaTracker(this);
bufferimage=createImage(getWidth(),getHeight());
gpb=bufferimage.getGraphics();
gpb.setColor(new Color(255,255,255));
for(int i=0;i<imgs.length;i++)
{
imgi[i]=getImage(urls,imgs[i]);
mt.addImage(imgi[i],i);
}
try
{
if(!mt.checkID(0,true))
mt.waitForID(0);
System.out.println("第一个图片载入完毕");
gpb.fillRect(0,0,getWidth(),getHeight());
gpb.drawImage(imgi[0],-1,-1,null);
}
catch(Exception ex){System.out.println("第一个图片载入失败");}
}
catch(Exception e){}
}
public void start()
{
locate=0;
}
public void paint(Graphics g)
{
if(locate==imgi.length)
locate=0;
if(locate%9==0)
System.gc();
System.out.println(imgs[locate]);
try
{
Thread.sleep(500);
if(!mt.checkID(locate,true))
mt.waitForID(locate);
}
catch(Exception e){}
g.drawImage(bufferimage,-1,-1,null);
gpb.fillRect(0,0,getWidth(),getHeight());
gpb.drawImage(imgi[locate++],-1,-1,null);
repaint();
}
public void update(Graphics g)
{
paint(g);
}
}
//<applet code="flashs.class" Width=400 Height=500>
//</applet>