-
Memory problem while printing png images
I am using the following program for printing png images (large images 300 dpi to 600 dpi). But its taking a lot of memory after printing some images and program is exiting with "You have less virtual memory " , "Unable to allocate memory"....
here is my print method
Code:
public int print (Graphics g, PageFormat pageFormat, int page){
try{
if(page >= totalPages)
return NO_SUCH_PAGE;
if(currentPage < 0 || currentPage != page){
image=null;
scale = -1;
System.gc();
setImage(page);}
Graphics2D g2d = (Graphics2D) g;
if(image != null){
double imageableWidth=pageFormat.getImageableWidth();
double imageableHeight=pageFormat.getImageableHeight();
double imageableX=pageFormat.getImageableX();
double imageableY=pageFormat.getImageableY();
if(scale < 0)
scale = Math.min(imageableWidth/width,imageableHeight/height);
g2d.translate(imageableX,imageableY);
if(scale<=1) { g2d.scale(scale,scale);}
if(height > 600) { divFactor = height / 600 ;}
newHeight = height/divFactor;
while(count<=divFactor){
BufferedImage im = image.getSubimage(0,y,width,newHeight);
g2d.drawImage(im,0,y,null);
im.flush();
im = null;
count++;
y += newHeight;
}
count = 1;
y = 0;
newHeight = 0;
divFactor = 1;
image.flush();
scale = -1;
}
else
System.out.println("Printing Error for page "+page);
g2d.dispose();
g2d = null;
g.dispose();
g = null;
currentPage = page;
}catch(OutOfMemoryError err)
{
System.out.println("OutOfMemoryError in Print: " + err);
return NO_SUCH_PAGE;
}
catch(Exception e){
System.out.println("(print ) ERROR: " + e);
return NO_SUCH_PAGE;
}
return Printable.PAGE_EXISTS;
}
and setImage method :
public void setImage(int pathcount) throws OutOfMemoryError {
try{
URL url = new URL(paths[pathcount]);
InputStream in = url.openStream();
//ImageInputStream imin = ImageIO.createImageInputStream(new BufferedInputStream(in));
ImageInputStream imin = ImageIO.createImageInputStream(in);
Iterator readers = ImageIO.getImageReaders(imin);
if (!readers.hasNext())
throw new IOException("No readers");
ImageReader reader = (ImageReader) readers.next();
reader.setInput(imin);
width = reader.getWidth(0);
height = reader.getHeight(0);
image = reader.read(0);
reader.abort();
reader.dispose();
reader = null;
readers = null;
image.flush();
imin = null;
in = null;
url = null;
}catch(OutOfMemoryError err)
{ System.out.println("OutOfMemoryError in Reading: " + err);
image = null;
}
catch(Exception e){
System.out.println("Error: " + e);
e.printStackTrace();
image = null;
}
}
paths is a global string array which contains png image urls and initial value of currentPage is -1 which used to call setImage only once per image
After printing some images memory usage increasing (using task manager->performance->PF Usage) a lot. I have 256MB RAM at first PF usage is begining at 150MB and increasing upto 610MB, at this point application is exiting. What i think is java objects are not garbage collecting and memory usage is increaing. Whats wrong i am doing here , How can i reduce the memory usage?
-
-you may free system resources ( Graphics )
where you explicitly got it .
( i guess the scope , you called print(..) )
this will make it simple to track them.
-no need to use System.gc() function , becos it will work
when its required, it can not recovery any problem calling directly.
Similar Threads
-
By vikassheelgupta in forum Java
Replies: 0
Last Post: 07-22-2005, 05:35 AM
-
Replies: 0
Last Post: 10-29-2001, 01:55 PM
-
By Michael Shutt in forum Web
Replies: 0
Last Post: 06-26-2001, 02:25 PM
-
By Michael Shutt in forum XML
Replies: 0
Last Post: 06-26-2001, 11:51 AM
-
Replies: 0
Last Post: 11-13-2000, 01:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|