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.

9
  • Well if you're never letting the map get garbage collected, and keep adding more entries to it, then sure, that's going to be a problem... Commented May 13, 2017 at 16:46
  • can GC be forced, or at least hinted? But that is again bad practice innit Commented May 13, 2017 at 16:47
  • I dont store over 10 files per report in memory, so per session usage shouldn't be high Commented May 13, 2017 at 16:49
  • We have no idea what "files" or "reports" you're talking about - you've basically given us almost no context here. What are you doing to clear entries out of the map? Commented May 13, 2017 at 16:51
  • @publicinformation do you ever remove the byte[] from that map? Commented May 13, 2017 at 16:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.