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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:47+00:00 2026-05-27T22:31:47+00:00

I am currently trying to solve a problem on one of the online programming

  • 0

I am currently trying to solve a problem on one of the online programming contests. The limit for the program is 64 megabyte in this contest.

I wrote a program in Java that has a section of fields in the class declararation that works like this:

private int[] sizes = new int[1024]; // 4096 bytes
private boolean[][] compat = new boolean[1024][1024]; // 128 kb
private boolean[][] compat2 = new boolean[1024][1024]; // 128 kb

private long[][][] dp = new long[29000][51][2]; // About 3*8 = 24 megabytes
private int [][] masks  = new int[29000][2]; // About 240 kb
private int avail = 0; 
private int avail2 = 0;
private int[] positions = new int[500000]; // About 2 megabytes
private int[][] ranges = new int[29000][2]; // About 240 kb
private int[][] maskToPos = new int[1024][1024]; // About 4 megabytes
private int[][][] init = new int[29000][51][2]; // About 3*4 = 12 megabytes

Now, the class has only a main procedure and some cycles inside it, without any additional arrays declared (just some variable to iterate through the cycles). However, then I tried to run this code on my local machine with the key -Xmx64m, I’ve got an OutOfMemoryError. It only managed to execute with the key -Xmx128m.

I also tried to lauch in on the online server, it gave the same error, and also gave additional info that my program used about 148460 kb.

But why so much? As far as I can calculate from the fragment above, it only should use about 40 megabytes. Is there something wrong with this calculation in the comments?

  • 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-27T22:31:48+00:00Added an answer on May 27, 2026 at 10:31 pm

    These two are the biggest killers:

    private long[][][] dp = new long[29000][51][2]; // About 3*8 = 24 megabytes
    private int[][][] init = new int[29000][51][2]; // About 3*4 = 12 megabytes
    

    Looking at the second, for example… that isn’t 12 megabytes. You’ve got 29000 int[][] objects, each of which contains references to 51 int[] objects, each of which contains 2 integers.

    Assuming a 32-bit reference size and 16 bytes of overhead for the array itself (length + common object overhead) that means the int[][] objects are each of size 51 * 4 + 16 = 220 bytes, and then the int[] objects are each of size 24 bytes. But you’ve got 29000 * 51 of those 24-byte objects – which is 35MB just in itself… Then there’s the 29000 int[][] objects, which is another 6MB… (Then there’s the top level array itself, but that’s only about 120K.)

    Basically, you need to remember that Java doesn’t have multi-dimensional arrays: it has arrays of arrays, and each array is an object, with separate overhead. I suggest you may want to use:

    private int[] init = new int[29000 * 51 * 2];
    

    instead, and work out appropriate offsets yourself. (Ditto for dp, which is even worse as it’s long values rather than int values, making each of the 29000 * 51 arrays take at least 32 bytes rather than 24.)

    Even just reversing the order in which you treat the dimensions would help:

    private long[][][] dp = new long[2][51][29000];
    private int[][][] init = new int[2][51][29000];
    

    Now for each of these variables, there’s one top-level array-of-arrays-of-arrays, 2 arrays-of-arrays, and 102 arrays of long or int. That corresponds to a lot less overhead.

    Your other calculations are incorrect too, but I think these two arrays-of-arrays-of-arrays are the worst.

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

Sidebar

Related Questions

I'm currently trying to pass a mono threaded program to multithread. This software do
I'm currently trying to solve the problem when I need to load rows from
I am trying to solve one problem from Liang's book I have done most
I'm currently trying out db4o (the java version) and I pretty much like what
I'm currently trying to get into the Java EE development with the Spring framework.
I am trying to solve a problem I have inherited with poor treatment of
I am trying to solve a consumer/producer problem, and I have to create three
I am trying to solve a problem in Objective-C, but I don't think the
I'm currently trying Backbone.js along with a rails app. My problem is, that I
I've been banging my head on walls trying to solve this. My app uses

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.