question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't catch OutOfMemoryError

See original GitHub issue

Gson currently catches OutOfMemoryErrors at two places and wraps them inside JsonParseException: https://github.com/google/gson/blob/c5a3f21fbad3828deba1deb88168e1f76829f807/gson/src/main/java/com/google/gson/JsonParser.java#L88-L89 https://github.com/google/gson/blob/b75e1bbc7915f409ba0254fcc92c71ed6798e69b/gson/src/main/java/com/google/gson/JsonStreamParser.java#L90-L91

This is bad practice because Gson is not necessarily the cause for this error and wrapping it inside an Exception subclass prevents the caller from noticing the OutOfMemoryError until later.

Edit: Catching OutOfMemoryError might be desired to protect about malicious JSON data, see also https://github.com/google/gson/pull/1912#issuecomment-1130767871. However, it appears OutOfMemoryError is not caught consistently everywhere.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lyubomyr-shaydarivcommented, Jun 3, 2020

@wind57 just to be clear: I didn’t design it, but I’m trying to reconstruct the rationale behind the design choice and trying to figure out why several errors (OOM is just one of them) are caught right there, despite most sources recommend never catch these but log them at the very top level (or just let the JVM to crash). Perhaps catching the OOM error might be fine when trying to allocate too lengthy memory blocks (like arrays I was thinking of above or any other objects that may require such arrays under the hood (huge bitmaps/images, huge strings, etc: https://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror)) and not work fine when trying to catch an OOM error while constructing a complex/fragmentary object built of too many small blocks like JsonElement hoping that GC would collect such a JSON tree once it fails to construct, so the process would survive. But yes, we’ll see.

1reaction
wind57commented, Jun 3, 2020

the argument against catching an OutOfMemory in any library code is strong. The only place I would be aware of where such a thing would potentially make sense is the very top level “catcher” in an application, for logging purposes only.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catching java.lang.OutOfMemoryError? - Stack Overflow
In OpenJdk1.7.0_40, I don't get any error or exception when I run this code. Even I changed the MEGABYTE to GIGABYTE (1024*1024* ...
Read more >
Why is it not a good idea to catch java.lang.OutOfMemoryError?
Yes, you technically can still catch them, like any other Throwable, but that . ... As long as you don't attempt to allocate...
Read more >
Java OutOfMemoryError Exceptions: Causes & Fixes [Tutorial]
The best solution is to analyze the places where you're trying to catch the OutOfMemoryError. Definitely avoid catching the mentioned error in ...
Read more >
3.2 Understand the OutOfMemoryError Exception
If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time. Instead,...
Read more >
Understanding OutOfMemoryError Exception in Java
If you do not set the sizes yourself, platform-specific defaults will be used. The java.lang.OutOfMemoryError: PermGen space error indicates ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found