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 6957957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:04:42+00:00 2026-05-27T15:04:42+00:00

The quoted code snippet from the JDK 7 java.util.ArrayList class confuses me. I can’t

  • 0

The quoted code snippet from the JDK 7 java.util.ArrayList class confuses me. I can’t for the life of me understand how can it possibly result in overflow. The areas which I’m confused in are marked with <--- what do they mean by this?. Can someone please help me out in understanding the circumstances under which this logic might overflow? TIA.

public void ensureCapacity(int minCapacity) {
    if (minCapacity > 0)
        ensureCapacityInternal(minCapacity);
}  

private void ensureCapacityInternal(int minCapacity) {
    modCount++;
    // overflow-conscious code <--- what do they mean by this?
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

private void grow(int minCapacity) {
    // overflow-conscious code <--- what do they mean by this?
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
}

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow <--- what do they mean by this?
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

EDIT: My main concern is: how can hugeCapacity ever receive a negative size?

  • 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-05-27T15:04:42+00:00Added an answer on May 27, 2026 at 3:04 pm

    The OutOfMemoryError() (in ArrayList) is thrown for 2 conditions:

    1. The minCapacity is less that zero. That means that a memory array allocation cannot be created, i.e. elementData array size cannot be less that zero.
    2. The capacity of the array has exceeeded the VM memory heap space. The VM cannot create memory (grow()) to allocate more object in the array.

    Hence why hugeCapacity() ensures that memory is allocated safely.


    grow() and hugeCapacity() is only used within ensureCapacityInternal(), so one can say that the minCapacity < 0 is unecessary. I believe it’s a safety check put in place, just to render the growing of array safe. Any discrepency, rather throw an error.

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

Sidebar

Related Questions

I have this code snippet <li class='<asp:Literal ID=litTest1 runat=server />'>stuff</li> On my computer, if
We have a project that generates a code snippet that can be used on
I have the following code: The actual problem is the non-quoted code. I want
I have this HTML code: <div class='com_box'> <div class='com_box_c'> <div class='com_box_info'> <a id='quote'>quote</a> </div>
$lineArray = preg_split('/\t\s*(?=([^]*[^]*)*[^]*$)/', $line); Above code snippet it to split a tab delimited file
In an Alexander Kuznetsov article , he presents the follow code snippet: CREATE TABLE
I don´t understand the tags that configure a custom sales quote. In this code
could some one please explain this snippet of magento code found in loadByCustomerId() in
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
Is it possible to modify NotifyIcon behavior to AlwaysShow in C#? A code snippet

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.