3

I am trying to figure out the size of a variable in php.

The reason i need to do this is because if it is over 1mb i need to use a different form of caching rather than memcache as memcache has a limit of 1mb.

I am using the below:

 $start_memory = memory_get_usage();
 $this->results= $dataReader->readAll();
 $this->end_memory = memory_get_usage() - $start_memory;

The results are coming back as 50mb which is very inaccurate. I have looked at str length also but the data is an array. Can someone let me know the most accurate way of detecting the size of a variable in php.

1
  • Languages that allow you to do this are the exception, and PHP is not one of them. You 'll have to attack the problem differently. Commented Jun 1, 2013 at 20:01

1 Answer 1

2

You can Use MultipartCache it's a simple class that extends memcache to help you split the arrays or string automatically

Simple Test

$cache = new Mcache\Main();
$cache->addserver("127.0.0.1");  // Local memecache server

$cache->set($key, file_get_contents("large_image.jpg"));

header("Content-Type: image/jpeg");
echo $cache->get($key); // large image for cache 

If you are just interested in the size of of your data then

$data = serialize($this->results);
echo strlen($data) . " bytes";
Sign up to request clarification or add additional context in comments.

1 Comment

this is what i am using- there is no point in using memcache though i guess for encoding decoding if the contents are less than 1mb- extra overhead. multipartcache great class though and very useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.