Page 1 of 1

Why is my class taking up 500K when the file is only 103K?

Posted: Fri Mar 02, 2007 10:14 pm
by MikeyCarter
I have a class that when I include it the memory_get_usage jumps by almost 500K. That's just after doing a include_once('filepath')

Any one know why or how I can find out why it's taking so much memory? I have another that the file is 187k but after doing the include_once the memory_get_usage jumps to 2M.

Any help please.

Posted: Fri Mar 02, 2007 10:28 pm
by me!
Whats in the class? Post it...

Posted: Fri Mar 02, 2007 11:16 pm
by MikeyCarter
Is there a way to attach the file?

Why would just including the file increase the size so much? None of the variables have been initialized.

Posted: Fri Mar 02, 2007 11:23 pm
by feyd
Declared variables require space as does the source code. Objects have additional overhead associated with their structures. It all must go somewhere.

It's rare that I'll have a file larger than at most 30-40K in source. Is it not possible to break apart these files to help modularize some of the information since you likely aren't using 100% of the code in each file during each request?

Posted: Sat Mar 03, 2007 1:54 am
by AKA Panama Jack
It's the nature of how objects work in PHP. I have seen classes use over 10 times the memory compared to the size of the program file.

When your object class is created it will allocate an ADDITIONAL block of memory for that classes code. If you create a second object PHP will allocate another block of memory for the second object almost doubling the usage of memory.

The class system under PHP, both 4 and 5, is not very efficient when it comes to memory usage.

That's why I think creating sites that use all classes is inefficient coding. You should use classes only where they are needed to reduce server load. Shared hosts, especially, will get ticked off if you start using too much memory and CPU from over-usage of classes.

Some people might not like to hear that. :D

But PHP class usage is like rich chocolate. It tastes good but too much will make you sick.

Maybe they will get the object model working better and less of a memory hog with PHP 6. Only time will tell.

Posted: Sat Mar 03, 2007 5:07 am
by onion2k
The size of the source code and the amount of memory it'll take up are pretty much unrelated. It's what the code is doing that fills up memory. For example:

Code: Select all

for ($x=1;$x<10000;$x++) {
	$arrStuff[] = "lets use some memory";
}
echo memory_get_usage();

Posted: Sat Mar 03, 2007 5:09 am
by Chris Corbyn
onion2k wrote:The size of the source code and the amount of memory it'll take up are pretty much unrelated. It's what the code is doing that fills up memory. For example:

Code: Select all

for ($x=1;$x<10000;$x++) {
	$arrStuff[] = "lets use some memory";
}
echo memory_get_usage();
Not strictly true. Big classes use lots of memory before you even create objects from them. And of course, a class by itself doesn't do anything at all :)

EDIT | viewtopic.php?t=64060

Note that PHP4 uses a lot less memory than PHP5. I really hope they can change this in PHP6.

Posted: Sat Mar 03, 2007 5:13 am
by onion2k
d11wtq wrote:Not strictly true. Big classes use lots of memory before you even create objects from them.
No, it is strictly true. When your class is parsed by PHP it'll create various internal bits and bobs that it'll need when it's instantiated. That takes memory. The memory footprint is directly related to what the code does, not how big the code is.

Posted: Sat Mar 03, 2007 5:14 am
by Chris Corbyn
Yes I can agree with that :) Apologies, I misinterpreted your code example :oops:

Posted: Sat Mar 03, 2007 7:40 am
by the_last_tamurai
ok...so If I have my index.php contains all objects instantiation ( like bootstrap in ZF )
this'll affect my site performance

Posted: Sat Mar 03, 2007 8:25 am
by onion2k
the_last_tamurai wrote:ok...so If I have my index.php contains all objects instantiation ( like bootstrap in ZF )
this'll affect my site performance
It's not really as simple as that. The point is that your code needs to do everything necessary to run your site and no more. Having code in there that's not used will adversely affect performance, having code in there that isn't making efficient use of resources (taking more memory than necessary for example), those sorts of things will adversely affect your site's performance.

But, that said, while it's a good idea not to waste resources, it's usually such a minimal difference it's not worth worrying about. On some of my sites I use a User object that's instantiated on every page, even when there's nothing that interacts with it in that particular script. Ok, it sucks up a few hundred kb more memory. The server copes fine, and it means that if ever I come to add some functionality to the page that needs it it's there already. You need to strike a balance between resources on the server and easy to maintain code. Just remember that your server can do a lot before you run into problems, so optimising to the nth degree is usually a little crazy.

Posted: Sat Mar 03, 2007 11:48 am
by AKA Panama Jack
the_last_tamurai wrote:ok...so If I have my index.php contains all objects instantiation ( like bootstrap in ZF )
this'll affect my site performance
It depends upon the SIZE of the objects. A number of SMALL objects will but far less strain on the CPU than having one massive object. The large object takes more CPU cycles to create because the operating system has to find and allocate memory for the entire object.

This code...

Code: Select all

class mine
{
	function test()
	{
		echo "Test";
	}
}

$object = new mine();
$object->test();
...will use a large chunk of memory compared to...

Code: Select all

function test()
{
	echo "Test";
}

test();

The both do the same thing but the class will allocate memory for the object when the object is created. The normal procedure outside the class will not do this. Using many classes and/or large classes will have a serious impact on the number of pages per second your site can serve. On sites that get very little activity this will not be noticeable. But if the site is very active you can see the server load skyrocket. If the software is running on a shared host they will usually shut you down. Not because you have a high number of people accessing the site but because the CPU and/or memory usage is too high. Be very careful about what classes you need loaded and only use those classes that are absolutely necessary. Most sites can get by without using any classes or just a small handful of small classes.