2

Is it a bad practice to store byte[] in a map as such

static LinkedHashMap<String, byte[]> fileBuffer = new LinkedHashMap<>()?

When my class is unloaded profiler still shows persitent byte[] memory usage.

Eventually OutOfMemoryError is thrown after several hours.

Does jvm (Oracle jdk8u121) have some prejudice regarding map GC?

Some context: A dynamic custom report tool using Oracle jobs on a server.

MT0
  • 143,790
  • 11
  • 59
  • 117
downvoteit
  • 588
  • 2
  • 7
  • 12

1 Answers1

1

My best bet is that you never clear your map, or you are never making ig garbage collectable prevending it from beeing garbage collected. Map holds strong referencess to byte buffers, so if map is not GC'd buffers are not as well

So is it bad practice? No, but bad practice is to hold your maps forever.

Consider using WeakReference for caching.

Map<String,WeakReference<byte[]>> will allow map values to be garbage collected despite map itself beeing non-collectable.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • I had a similar idea, because I kill the reference to my map the byte array reference is rendered dubious for GCing. I will test what you recommended, thanks – downvoteit May 13 '17 at 17:39