leonbloy / pngj

PNGJ : pure Java library for high performance PNG encoding
http://hjg.com.ar/pngj/
278 stars 51 forks source link

Memory efficiency in FilterType[] enum #21

Closed leonbloy closed 9 years ago

leonbloy commented 9 years ago

From zelonew...@gmail.com on January 16, 2013 01:22:46

I am working on an application that loads many PNG images very rapidly and I am trying to decrease memory allocations to meet garbage collector performance constraints in other parts of the application. As such, I am planning to switch from ImageIO.read(..) to pngj as a more efficient alternative.

I have been profiling pngj under visualvm with a test application that reads the same png file over and over in an infinite loop. I noticed an unexpected source of unexpected memory allocations in ar.com.hjg.pngj.FilterType. In particular, the use of values() in getByVal(int) creates an extraneous instance of FilterType[]. I made a tiny change to store the result of values() in a static variable that makes the extra allocations go away.

My updated FilterType class is attached.

Attachment: FilterType.java

Original issue: http://code.google.com/p/pngj/issues/detail?id=21

leonbloy commented 9 years ago

From hgonzalez@gmail.com on January 16, 2013 16:20:50

Status: Accepted

leonbloy commented 9 years ago

From hgonzalez@gmail.com on January 16, 2013 16:21:05

Owner: hgonzalez@gmail.com

leonbloy commented 9 years ago

From hgonzalez@gmail.com on January 16, 2013 18:02:03

a map is statically created now https://code.google.com/p/pngj/source/detail?r=8b643278508c5f37cc8422790db9d885c5adb673

Status: Fixed