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

  • Home
  • SEARCH
  • 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 78805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:02:41+00:00 2026-05-10T21:02:41+00:00

I recently came across this in some code – basically someone trying to create

  • 0

I recently came across this in some code – basically someone trying to create a large object, coping when there’s not enough heap to create it:

try {     // try to perform an operation using a huge in-memory array     byte[] massiveArray = new byte[BIG_NUMBER]; } catch (OutOfMemoryError oome) {     // perform the operation in some slower but less     // memory intensive way... } 

This doesn’t seem right, since Sun themselves recommend that you shouldn’t try to catch Error or its subclasses. We discussed it, and another idea that came up was explicitly checking for free heap:

if (Runtime.getRuntime().freeMemory() > SOME_MEMORY) {     // quick memory-intensive approach } else {     // slower, less demanding approach } 

Again, this seems unsatisfactory – particularly in that picking a value for SOME_MEMORY is difficult to easily relate to the job in question: for some arbitrary large object, how can I estimate how much memory its instantiation might need?

Is there a better way of doing this? Is it even possible in Java, or is any idea of managing memory below the abstraction level of the language itself?

Edit 1: in the first example, it might actually be feasible to estimate the amount of memory a byte[] of a given length might occupy, but is there a more generic way that extends to arbitrary large objects?

Edit 2: as @erickson points out, there are ways to estimate the size of an object once it’s created, but (ignoring a statistical approach based on previous object sizes) is there a way of doing so for yet-uncreated objects?

There also seems to be some debate as to whether it’s reasonable to catch OutOfMemoryError – anyone know anything conclusive?

  • 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. 2026-05-10T21:02:42+00:00Added an answer on May 10, 2026 at 9:02 pm

    freeMemory isn’t quite right. You’d also have to add maxMemory()-totalMemory(). e.g. assuming you start up the VM with max-memory=100M, the JVM may at the time of your method call only be using (from the OS) 50M. Of that, let’s say 30M is actually in use by the JVM. That means you’ll show 20M free (roughly, because we’re only talking about the heap here), but if you try to make your larger object, it’ll attempt to grab the other 50M its contract allows it to take from the OS before giving up and erroring. So you’d actually (theoretically) have 70M available.

    To make this more complicated, the 30M it reports as in use in the above example includes stuff that may be eligible for garbage collection. So you may actually have more memory available, if it hits the ceiling it’ll try to run a GC to free more memory.

    You can try to get around this bit by manually triggering a System.GC, except that that’s not such a terribly good thing to do because

    -it’s not guaranteed to run immediately

    -it will stop everything in its tracks while it runs

    Your best bet (assuming you can’t easily rewrite your algorithm to deal with smaller memory chunks, or write to a memory-mapped file, or something less memory intensive) might be to do a safe rough estimate of the memory needed and insure that it’s available before you run your function.

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

Sidebar

Ask A Question

Stats

  • Questions 167k
  • Answers 167k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From memory: from select where join on equals group by… May 12, 2026 at 1:36 pm
  • Editorial Team
    Editorial Team added an answer Try it in Firefox with the Firebug add-on, with the… May 12, 2026 at 1:36 pm
  • Editorial Team
    Editorial Team added an answer In the example you listed, the Maps API was requested… May 12, 2026 at 1:36 pm

Related Questions

I recently came across this in some code - basically someone trying to create
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
This is my first StackOverflow question so be nice! :-) I recently came across
The sad truth about this post is that I have poor regex skills. I
Imagine an object you are working with has a collection of other objects associated

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.