Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Memory footprint way too large

I have tried the memory usage of some simple variables and encounter unexpected results, please see this code:

        $datetimes = [];
        $memory_before = memory_get_usage();

        for ($x = 0; $x < 1000; $x++) {
            $datetimes[] = new \DateTime();
        }
        var_dump('DateTimes: ' . (memory_get_usage() - $memory_before));


        $ints = [];
        $memory_before = memory_get_usage();

        for ($x = 0; $x < 1000; $x++) {
            $ints[] = $x;
        }
        var_dump('Integers: ' . (memory_get_usage() - $memory_before));

I get this output (on PHP 7.4, 64bit):

string(17) "DateTimes: 350504"
string(15) "Integers: 37160"

37 KB memory for 1000 ints does not make sense to me, right? I'd expect 8000 byte plus some array overhead.

My experiment scales: for a million ints, I get 33558808 byte memory usage.

I have disabled xdebug.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's how PHP works and the disadvantage of having dynamically-typed variables.

The integer is in reality a Zend object.

1000 x (64 * 2) = 128 Kbit so 16KB.

Add to that the array of size 1000.

In memory, zval is represented as two 64-bit words. The first word keeps the value — and the second word keeps the type, type_flags, extra, and reserved fields.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!