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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:09:41+00:00 2026-06-13T05:09:41+00:00

An int array of 100 million ints should be 400 MB, right? One int

  • 0

An int array of 100 million ints should be 400 MB, right?
One int = 4 bytes

So if I set the VM to have a max heap of 1024m with -Xmx1024m why does this code fail:

public static void main(String[] args) {

    int[] b = new int[100000000]; //100 mil = 400mb
    System.out.println("ok");
    int[] c = new int[100000000];

}

This is the output:

ok
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Main.main(Main.java:9)

Solution – break it up!

public static void main(String[] args) {

    int[] a = new int[50000000]; //50 mil = 200mb
    System.out.println("ok 1");
    int[] b = new int[50000000];
    System.out.println("ok 2");
    int[] c = new int[50000000];
    System.out.println("ok 3");
    int[] d = new int[50000000];
    System.out.println("ok 4");


}
  • 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-13T05:09:42+00:00Added an answer on June 13, 2026 at 5:09 am

    The problem lies in the fact that when you’re creating this int[], it requires contiguous memory of 400mb. Basically this means, you need a solid block of 400mb of memory for use. Due to fragmentation of the heap, it can’t find a solid 400mb block of memory to use. That’s why you’re getting the out of memory error in this situation.

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

Sidebar

Related Questions

I have an int array for which i have allocated space for 100 elements.
I have an int array int[] myArray = new int[100]; and want to get
I have millions of fixed-size (100) int arrays. Each array is sorted and has
i have two int array of images , i have implemented it in grid
I have an int array containing gray scale values from 0-254, i also have
I have an int array which has no elements and I'm trying to check
I have a structure that has an array. struct Page_Directory { public: int numEntries;
A book I have says this: a) Place each value of the one-dimensional array
I have an array of a million integers because I am experimenting with parallel
const int i = 100; int *j = &i; int array[i] = {0}; Is

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.