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

The Archive Base Latest Questions

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

Here is my code, which creates 2d array filled with zeros, array dimensions are

  • 0

Here is my code, which creates 2d array filled with zeros, array dimensions are (795,6942):

function zeros($rowCount, $colCount){
    $matrix = array();
    for ($rowIndx=0; $rowIndx<$rowCount; $rowIndx++){
        $matrix[] = array();
        for($colIndx=0; $colIndx<$colCount; $colIndx++){
            $matrix[$rowIndx][$colIndx]=0;
        }
    }
    return $matrix;
}

$matrix = zeros(795,6942);

And here is the error that I receive:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes)

Any ideas how to solve this?

  • 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-13T22:34:47+00:00Added an answer on May 13, 2026 at 10:34 pm

    As a quick calculation, you are trying to create an array that contains :

    795*6942 = 5,518,890
    

    integers.

    If we consider that one integer is stored on 4 bytes (i.e. 32 bits ; using PHP, it not be less), it means :

    5518890*4 = 22,075,560
    

    bytes.

    OK, quick calculation… result is “it should be OK“.

    But things are not that easy, unfortunatly 🙁

    I suppose it’s related to the fact that data is stored by PHP using an internal data-structure that’s much more complicated than a plain 32 bits integer

    Now, just to be curious, let’s modify your function so it outputs how much memory is used at the end of each one of the outer for-loop :

    function zeros($rowCount, $colCount){
        $matrix = array();
        for ($rowIndx=0; $rowIndx<$rowCount; $rowIndx++){
            $matrix[] = array();
            for($colIndx=0; $colIndx<$colCount; $colIndx++){
                $matrix[$rowIndx][$colIndx]=0;
            }
            var_dump(memory_get_usage());
        }
        return $matrix;
    }
    

    With this, I’m getting this kind of output (PHP 5.3.2-dev on a 64bits system ; memory_limit is set to 128MB — which is already a lot !) :

    int 1631968
    int 2641888
    int 3651808
    ...
    ...
    int 132924168
    int 133934088
    Fatal error: Allowed memory size of 134217728 bytes exhausted
    

    Which means each iteration of the outer for-loop requires something like 1.5 MB of memory — and I only get to 131 iterations before the script runs out of memory ; and not 765 like you wanted.

    Considering you set your memory_limit to 128M, you’d have to set it to something really much higher — like

    128*(765/131) = 747 MB
    

    Well, even with

    ini_set('memory_limit', '750M');
    

    it’s still not enough… with 800MB, it seems enough 😉

    But I would definitly not recommend setting memory_limit to such a high value !

    (If you have 2GB of RAM, your server will not be able to handle more than 2 concurrent users ^^ ;; I wouldn’t actually test this if my computer had 2GB of RAM, to be honest)

    The only solution I see here is for you to re-think your design : there has to be something else you can do than use this portion of code 🙂

    (BTW : maybe “re-think your design” means using another language PHP : PHP is great when it comes to developping web-sites, but is not suited to every kind of problem)

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use trim to remove the extra spaces. With… May 15, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer If you want the current time, just: dateval += DateTime.Now.TimeOfDay;… May 15, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer Defining the relationship What you're defining is a unidirectional one-to-many,… May 15, 2026 at 12:29 pm

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.