I am developing a swing application where i am displaying profile information along with their photo, after loading about 120 photos i get the exception java.lang.OutOfMemoryError: Java heap space I need to display around 1000+ profile informations. This is how i load my images onto the jtable
try{
byte[] imageAsByteArray = getImageAsByteArray("E:\\Project\\WinPak\\Database\\UserImage\\"+employee.getLink3().trim()+"-1.jpg");
if(imageAsByteArray != null)
{
InputStream imageInputStream =new ByteArrayInputStream(imageAsByteArray);
Image img = ImageIO.read(imageInputStream);
ImageIcon imgIcon = new ImageIcon(img.getScaledInstance(100,100,Image.SCALE_AREA_AVERAGING));
data[index][10] =imgIcon; // data is of type object which i use to populate the jtable
imageInputStream.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
public byte[] getImageAsByteArray(String url)
{
try
{
InputStream is = new BufferedInputStream(new FileInputStream(url));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
is.close();
return buffer.toByteArray();
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
How do i overcome this problem. Is there any other way to display the information in swing?