Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:35:43+00:00 2026-06-11T23:35:43+00:00

Many a carefully crafted piece of Java code has been laid to waste by

  • 0

Many a carefully crafted piece of Java code has been laid to waste by java.lang.OutOfMemoryError. There seems to be no relief from it, even production class code gets downed by it.

The question I wish to ask is: are there good programming/architecture practices where you can avoid hitting this error.

So the tools at a Java programmers disposal seem to be:

  1. java.lang.Runtime.addShutdownHook(Thread hook) — shutdown hooks allow for a graceful fall.
  2. java.lang.Runtime.freeMemory() — allows us to check the memory available to the VM

So the thought I had is: could one write factory methods which before the creation of objects check if the system has adequate memory left before attempting to allocate memory? For example in C, the malloc would fail and you would know that you had run out of memory, not an ideal situation but you wouldn’t just drop dead from an java.lang.OutOfMemoryError aneurism.

A suggested approach is to do better memory management or plug memory leaks or simply allocate more memory — I do agree these are valuable points but lets look at the following scenarios:

  1. I’m running on an Amazon micro instance
  2. I can allocate very little memory to my VM say 400M
  3. My Java process processes jobs in a multi-threaded fashion, each thread consumes a variable amount of memory depending on the parameters of the computational task
  4. Let’s assume that my process has no memory leaks
  5. Now if I keep feeding it jobs before they are complete it will eventually die of memory starvation
  6. If I set -Xmx too high — I’ll get swapping and possibly thrashing on the OS
  7. If I set an upper concurrently limit — that might not be optimal as I could be limiting the accepting of a job which could be executed with the available RAM or worse accept a job that requires a LOT of memory and end up hitting java.lang.OutOfMemoryError anyway.
    X. Hope that helps explain the motivation of the question — I think the standard responses are not mutually exclusive to seeking a fault tolerant approach to the problem.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:35:44+00:00Added an answer on June 11, 2026 at 11:35 pm

    We handled JVM memory more as a tuning parameter, than as something to manage actively with the application. We have a MemoryInfo class (which wraps several of the Runtime memory info methods).

    While application is running, we track free memory in the application as:

     Runtime.getMaxMemory() - Runtime.getTotalMemory() + Runtime.getFreeMemory();
    

    Max memory is the -Xmx jvm arg, total memory is what JVM has already allocated to the application heap, and free memory is how much of the allocated heap memory still available. (If your -Xms parameter is the same as your -Xmx parameter, then getFreeMemory() is all you need to check).

    If we get above 70% memory usage, we send alerts to our monitoring system. At that point we make a decision whether we can limp through the rest of the day, or whether adjust the -Xmx parameter and restart. Although this seems a bit messy, in practice, once we have tuned a system, we never run into memory problems after that. (Once you get above 90% max memory used, the JVM will GC extremely frequently to try to prevent running out of memory).

    I think the approach of managing memory with every construction is draconion, but if you need absolute control, then maybe it makes sense. Another approach is to make sure that any memory caches you use have an LRU or Expiration and reload mechanism, so you can better limit the number objects preserved in memory.

    That said, our approach is to keep as much as possible in memory, and just allocate plenty of RAM. Our big systems have 28G RAM allocated (we use betwen 40-60% of that on average).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked so many times before but I couldn't
many Places in the sample code i have seen 2 different way of @synthesize
Many ARIA demonstration websites use code such as: <label for=name id=label-name>Your Name</label> <input id=name
My app has a registration page for new users and I'm trying to carefully
A repeating theme in my development work has been the use of or creation
I am following the instructions very carefully from what seems to be the latest
See following code carefully. Because it works perfectly. Try to add in your application.
I've set up my code to carefully load and process data locally on my
I enjoy creating carefully crafted HTML/CSS e-mail signatures for my clients. My current method
Edit: I need to refactor this question, it has too many extraneous components to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.