Manage arrays and memory in php-php Tutorial

Source: Internet
Author: User
As mentioned in the manual about the management of arrays and memory in php, all types of variables except object in php are copies of them in assignment, parameter transfer, and return values.
But looking at the test below, I don't know if there is a problem with the test method, or if it doesn't work & share the same part of memory when passing by reference, but after changing its elements, a copy is generated?




Reply to discussion (solution)

What is your goal?

M = 0; $ m = memory_get_usage (); $ t = array ('id' => 1, 'name' => 'None'); echo '$ t usage: ', memory_get_usage ()-$ m, PHP_EOL; $ data = array (); echo' empty $ data Usage: ', memory_get_usage ()-$ m, PHP_EOL; $ m = memory_get_usage (); for ($ I = 0; I I <1000; $ I ++) {$ data [] = null ;} echo '$ data usage of 1000 null elements: ', memory_get_usage ()-$ m, PHP_EOL; $ data = array (); $ m = memory_get_usage (); for ($ I = 0; $ I <1000; $ I ++) {$ data [] = $ t;} echo '$ data usage of 1000 $ telements :', memory_get_usage ()-$ m, PHP_EOL; $ data = array (); $ m = memory_get_usage (); for ($ I = 0; $ I <1000; $ I ++) {$ data [] = array ('id' => 1, 'name' => 'None');} echo '$ data usage of 1000 entity elements :', memory_get_usage ()-$ m, PHP_EOL;
$ T usage: 360
Null $ data Usage: 496
$ Data Usage of 1000 null elements: 84200
$ Data Usage of 1000 $ telements: 52104
$ Data Usage of 1000 entity elements: 356136

This may be clearer.

Actually, I want to know if it is necessary to use reference transfer to improve performance when passing parameters or returning an array with a large size.

Of course it is a superficial phenomenon. I want to know how php manages the memory. Is there any official information? do you have to look at the php source?

I want to know how php manages the memory. of course, the source code is the best.
There are also some related articles on the Internet, which are not suitable for some years. After all, the kernel has been modified since php5.3.6. Of course, the speed has also increased by several orders of magnitude.

But he does not make much sense to study.
You only need to know that the variable is stored in a variable table at most.

Of course it is a superficial phenomenon. I want to know how php manages the memory. Is there any official information? do you have to look at the php source?

Hash table

/* Variable storage structure */struct _ zval_struct {zvalue_value value;/* Variable value */zend_uint refcount _ gc; zend_uchar type; /* current data type of the variable */zend_uchar is_ref _ gc;}; typedef struct _ zval_struct zval;/* storage structure of variable values */typedef union _ zvalue_value {long lval; /* long integer */double dval;/* double floating point type */struct {/* character type */char * val; int len;} str; HashTable * ht; /* use a hash table (a pointer) to store arrays */zend_object_value obj;/* object */} zvalue_value;

Php variables are copied at write time.
Look at this article: http://www.laruence.com/2008/09/19/520.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.