Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why is this code not cleaning up int arrays? or How to detect animage is a solid color?

Reply
Thread Tools

Why is this code not cleaning up int arrays? or How to detect animage is a solid color?

 
 
jw
Guest
Posts: n/a
 
      07-30-2008
Hi All,

I've got one idea I'm currently trying to get this code to better use
its int arrays, but as written below the system runs out of memory
after about 6 hours of crunching. The code below is in a loop called
hundreds of thousands of times, and I'm 97% sure the imgdata int array
is not getting properly cleaned up. While I'm waiting to see if my
idea fixed the issue I thought I'd ask if anyone here has a good idea.

This code splits up a large image into smaller images and saves each
subimage. The problem code is designed to inspect every 3rd pixel of
an image and if they are all the same, we assume the image is just a
solid color so instead of saving the entire image, we check a cache of
1x1 images for a 1x1 image of the matching color or else we scale it
to 1x1, put it into the cache and in either case and we set it up to
save the 1x1 image further below. When I commented out the code that
states "//begin area of code that is the issue" the system did not run
out of memory. Anyway, an alternate question is: Is there a better way
to detect if the image is just a solid color?

Thanks in advance for your help!

BufferedImage subImage;
ByteArrayOutputStream baos;
int[] imgdata; //variable that is the issue
for(int i = 0; i < w.tcnt; i++) {
for (int j = 0; j < w.tcnt; j++) {
//begin area of code that is the issue
subImage = w.img.getSubimage(i * tilesize, j * tilesize, tilesize,
tilesize);
imgdata =
((DataBufferInt)subImage.getRaster().getDataBuffer ()).getData();
if(imgdata.length>0) {
int firstpixel = imgdata[0];
int length = imgdata.length;
while((length=length-3)>0) {
if(firstpixel!=imgdata[length]) {
break;
}
}
if(length<4) {
if(images1x1.containsKey(firstpixel)) {
subImage = images1x1.get(firstpixel);
}
else {
subImage = subImage.getSubimage(0,0,1,1);
images1x1.put(firstpixel, subImage);
}
}
}
//end code that is the issue
baos = new ByteArrayOutputStream();
try {
ImageIO.write(subImage, "PNG", baos);
//internal call to save the image
} catch (IOException e) {
System.err.println("Error converting tile!");
}
}
}
subImage = null;
baos = null;
imgdata = null;
w.img = null;
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      07-30-2008
jw wrote:
> Hi All,
>
> I've got one idea I'm currently trying to get this code to better use
> its int arrays, but as written below the system runs out of memory
> after about 6 hours of crunching. The code below is in a loop called
> hundreds of thousands of times, and I'm 97% sure the imgdata int array
> is not getting properly cleaned up. While I'm waiting to see if my
> idea fixed the issue I thought I'd ask if anyone here has a good idea.
>
> This code splits up a large image into smaller images and saves each
> subimage. The problem code is designed to inspect every 3rd pixel of
> an image and if they are all the same, we assume the image is just a
> solid color so instead of saving the entire image, we check a cache of
> 1x1 images for a 1x1 image of the matching color or else we scale it
> to 1x1, put it into the cache and in either case and we set it up to
> save the 1x1 image further below. When I commented out the code that
> states "//begin area of code that is the issue" the system did not run
> out of memory. Anyway, an alternate question is: Is there a better way
> to detect if the image is just a solid color?
>
> Thanks in advance for your help!
>
> BufferedImage subImage;
> ByteArrayOutputStream baos;
> int[] imgdata; //variable that is the issue
> for(int i = 0; i < w.tcnt; i++) {
> for (int j = 0; j < w.tcnt; j++) {
> //begin area of code that is the issue
> subImage = w.img.getSubimage(i * tilesize, j * tilesize, tilesize,
> tilesize);
> imgdata =
> ((DataBufferInt)subImage.getRaster().getDataBuffer ()).getData();
> if(imgdata.length>0) {
> int firstpixel = imgdata[0];
> int length = imgdata.length;
> while((length=length-3)>0) {
> if(firstpixel!=imgdata[length]) {
> break;
> }
> }
> if(length<4) {
> if(images1x1.containsKey(firstpixel)) {
> subImage = images1x1.get(firstpixel);
> }
> else {
> subImage = subImage.getSubimage(0,0,1,1);
> images1x1.put(firstpixel, subImage);


I guess images1x1 is a HashMap (defined with a suitably large initial
size). As firstpixel is an int and int has around 4 billion possible
values this object could conceivably grow quite large.

Maybe you could check (assert?)
- that the value of firstpixel is within the range you expect.
- that images1x1.size() is less than some reasonable limit.


> }
> }
> }
> //end code that is the issue
> baos = new ByteArrayOutputStream();
> try {
> ImageIO.write(subImage, "PNG", baos);
> //internal call to save the image
> } catch (IOException e) {
> System.err.println("Error converting tile!");
> }
> }
> }
> subImage = null;
> baos = null;
> imgdata = null;
> w.img = null;



--
RGB
 
Reply With Quote
 
 
 
 
jw
Guest
Posts: n/a
 
      07-31-2008
Thanks for the suggestion RGB, but I know because of the images I'm
working with there are only about 7-8 possible solid color images.

Anyway, I first tried moved the declaration of the int array up,
hoping that would force the system to reuse teh same memory area each
time through the loop, but that didn't make a difference.

Then I made two other changes, and I'm not sure which was the actual
fix. I explicitly called System.gc() every 100th time through the
loop. Secondly I made a more fundamental change to the algorithm. I
switched from getting the BufferedImage's int array to repeatedly
using the BufferedImage's getRGB(x,y) method to get a single pixel of
info. When I wrote this before, I don't know if that method couldn't
be used for some reason (different image type for example) or if I
thought accessing the int array would be faster, or I just didn't know
about the method. Anyway, I didn't test these separately, so I don't
know which one made a difference, but I did want to post a follow up
for anyone interested or for future reference.
 
Reply With Quote
 
jw
Guest
Posts: n/a
 
      07-31-2008
Forgot to mention I did echo out the size of the Hashmap as suggested
and it never grew larger than 8 objects, just to be sure.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-01-2008
bugbear wrote:
> jw wrote:
>>


>> thought accessing the int array would be faster, or I just didn't know
>> about the method. Anyway, I didn't test these separately, so I don't
>> know which one made a difference.

>
> assuming it's not too hard, it would be helpful if you did,
> to people searching the USENET archive for similar
> issues.


Yes, and post an SSCCE of both. This is supposed to be a community
helping each other, not a tech support line. If you don't post up your
solutions, how the can the rest of us benefit from your discoveries? It
puts you in the position of only asking for help and never giving any.

I'd like to see a SSCCE of both myself, so I can understand better what
the original problem was....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments