Results 1 to 13 of 13
Thread: Java Memory Issue
- 05-17-2010, 08:23 PM #1
Java Memory Issue
I have a website that needs to upload images. My heap size is set to -Xmx352m. I was doing performance testing using jmeter uploading 10 concurrent 5mb files. I'd think everything would be ok since 10 * 5mb < 352mb. I'd notice that I'd get out of memory exceptions. I tried setting -Xmn96m (10 * 5mb < 96mb) for the "young" generation but still get errors.
BTW this is what I'm using for my
$JAVA_OPTS='-Xmx352m -Xmn96m -XX:MaxPermSize=256m'
- 05-18-2010, 09:12 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Are you turning the input stream into an Image in memory?
If so then that Image will be uncompressed and consequently much larger than 5mb.
If you want to check it out take a heap dump and look after you've read in 8 images.
- 05-18-2010, 12:56 PM #3
thanks
I actually am. I read the image in memory so that I can compress it into something smaller, so that later pages with multiple images load much faster. do you know if there is some ratio of compression for some file types?
- 05-18-2010, 01:46 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
No idea.
But I have seen pretty big increases (orders of magnitude).
Just do a heap dump and analyse it.
Should be pretty clear how big the Image objects are getting.
Are you holding the compressed stuff in memory?
That is, are you reading in the image, creating an Image object and then scaling?
- 05-18-2010, 08:32 PM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Decompressed images can easily be 10x or even 100x bigger than the compressed version if it's lossy compression (which is typical).
- 05-20-2010, 06:47 AM #6
thbks for the replies
yes I am holding the compressed image in memory. then writing to the fs. I can post the code. hopefully I can get some advice on how to improve things
- 05-20-2010, 08:51 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're writing to the filesystem, then discarding the image?
- 05-20-2010, 10:49 AM #8
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Are you using JApplet?
If so, put the following
<applet code=YourApplication.class name=YourApplication width=10% height=10% >
<param name="java_arguments" value="-Xms128m -Xmx256m">
Use BufferedImage and then when you are done processing that image don't forget to flush() and then set to null
If you are using Graphics2D then don't forget to dispose() after you are done painting.
- 05-20-2010, 11:14 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
In almost all instances setting something to null is a pointless exercise.
Just let it go out of scope.
- 05-21-2010, 06:31 AM #10
code
to answer peoples questions. its not an applet. i save the file to a tmp dir then read it in and resize.
i was talking to a co worker and he mentioned that i should not do this in java and instead call out to another program to do the resize so i don't have to deal with java memory issues. thoughts on that approach? any suggested command line tools?
Java Code:// load image from INFILE Image image = ImageIO.read(oldImage); // determine image size from WIDTH and HEIGHT int newWidth = imageSize.getWidth(); int newHeight = imageSize.getHeight(); double ratio = (double) newWidth / (double) newHeight; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double) imageWidth / (double) imageHeight; if (ratio < imageRatio) { newHeight = (int) (newWidth / imageRatio); } else { newWidth = (int) (newHeight * imageRatio); } // draw original image to new image object and // scale it to the new size on-the-fly BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = newImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, newWidth, newHeight, null); // save image to storage String newFileName = createJPGName(oldImage.getName()); // creates empty file object with new destination File newFile = _fileStorage.createFile(newFileName, destinationDir); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder. getDefaultJPEGEncodeParam(newImage); int quality = imageSize.getQuality(); quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float) quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(newImage); graphics2D.dispose(); out.close(); image = null; newImage = null;
- 05-21-2010, 09:05 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Wish I had my resizing code to hand so I could compare what I did end of last year with what you're doing.
It resized an uploaded image and stored that as a file (essentially what you're doing) and there wasn't any obvious memory issues.
I'd seriously consider taking that heap dump and simply looking at what's holding the memory. Something somewhere is being held. Oh, and setting things to null is largely pointless. If this is a self contained method then they'll be eligible for gc when the method exits.
- 01-07-2012, 06:04 AM #12
Member
- Join Date
- Jan 2012
- Location
- United States
- Posts
- 1
- Rep Power
- 0
Natalie Waters
Negative news - Syria's 'mutilation mystery' deepens...
- 01-07-2012, 02:05 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
help required to solve jasper report memory issue
By kiranrajan in forum Advanced JavaReplies: 0Last Post: 11-04-2009, 09:25 AM -
memory game in JAVA
By lclclc in forum New To JavaReplies: 19Last Post: 10-18-2009, 04:41 PM -
Memory issue in servlet application in a tomcat container
By bedhinesh in forum Java ServletReplies: 2Last Post: 07-24-2009, 01:26 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
Java Heap Out of Memory Error
By stonkers in forum New To JavaReplies: 3Last Post: 07-17-2007, 04:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks