Copy on write

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi list!

function foo() {
  $filecontents = file_get_contents('very_large_file');
  $data = unserialize(substr($filecontents,10));
  return $data;

Will PHP just adjust the pointer to the first character of
$filecontents or will it copy the contents around in RAM?


Re: Copy on write

Quoted text here. Click to load it


Are you sure you're doing what you think you're doing?  The
serialization format is very particular, and if not correct, will
raise a notice and not give you the data back.

Quoted text here. Click to load it

There are no pointers in PHP.  Why do you need to know how memory is
allocated?  What is it you're actually trying to accomplish?

Quoted text here. Click to load it

$email = str_replace('sig.invalid', '', $from);

Anonymous (1984 IOCCC winner):
int i;main()"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p)

Re: Copy on write

Curtis Dyer wrote:

Quoted text here. Click to load it
Of cpourse there are pointers: They are just hidden from view.
Quoted text here. Click to load it

Re: Copy on write

André Hänsel escribió:
Quoted text here. Click to load it

You can use memory_get_usage() to test it yourself. You'll see that
filling a variable with a very large string results in a very large RAM

If you want to read the first characters of a very large file you'd
better use the regular fopen() and fread() functions.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Re: Copy on write

Quoted text here. Click to load it

the substr($filecontents,10) function will create a new string.

As Curtis points out, tampering with the internal structure of
serialized data can be dangerous - so that's another reason for only
keeping the serialized stuff in the file - any metas data should be
moved elsewhere.

Also - I'd be wary of storing large amounts of data in serialized form
- previuosly I'd worked on an application which used a v. large and
complex tree structure which was originally stored in an array - the
app only needed to locate one or two leaf nodes - it was surpising how
quickly it became more efficient to build the tree backwards from the
required leaf using multiple database queries to establish the parent



Site Timeline