473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Do parent class share memory with extended classes?

guillermobytes
Hello,

I was wondering the cost of creating an instance of an extended class.

By that I mean, if I have classes:
- A
- A1 and A2,
where: A1 and A2 extend class A.

what do instances A1 and A2 share of their common parent A in memory?

a) Nothing
b) Attributes
c) just Methods
d) Attributes and Methods

by that I would like to know what has to be put in memory again?
My guess is c) but I don't know for sure, and I think that I also depends on the lifetime of the two instances, if they overlap or not. If they don't everything has to be put in to memory again... doesn't it?

And by the way, when I create two instances of the same class do they share the same amount of data than two subclasses with their parent (example above)?

Thanks

Regards,

bilibytes
Jan 24 '11 #1
4 2000
dlite922
1,584 Expert 1GB
I believe that is not the case. The parent class's memory is not shared between its children. Each one makes a copy for the object. Which makes sense because you do not want two objects to have identical data in them. I'm sure PHP has memory optimization methods and tricks.

Consider the simple example below.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Foo {
  3.    public $bar;
  4.  
  5. }
  6.  
  7. class Baz1 extends Foo {
  8.    public $mine; 
  9.    function __constructor($val) { $this->bar = $val;}
  10. }
  11.  
  12. class Baz2 extends Foo {
  13.    public $mine; 
  14.    function __constructor($val) { $this->bar = $val;}
  15. }
  16.  
  17. $test1 = new Baz1('Test One');  //sets its $bar to 'Test One'
  18. $test2 = new Baz2('Test Two'); // sets its $bar to 'Test Two'
  19.  
  20. echo $test1->bar; // outputs 'Test One'
  21. echo $test2->bar; // outputs 'Test Two'
  22.  
  23.  
As you can see above, if the Foo class was in shared memory both would have output: 'Test Two' (The last value assigned to Foo->$bar). But that is not the case. Which means Foo->bar is in memory twice with two different values.

Cheers,


Dan
Jan 26 '11 #2
dlite922
1,584 Expert 1GB
My guess at memory allocation:

Assuming Foo class needs 64 Bytes and for some reason Baz1 and Baz2 class need 32 and 16 bytes, respectively. Then the following memory would be needed for $test1 and $test2 objects:

$test1 = (64+32) = 98 Bytes
$test2 = (64+16) = 80 Bytes

If a PHP expert can comment, that would be fantastic!

Dan
Jan 26 '11 #3
Markus
6,050 Expert 4TB
Dan is right. Creating 2 objects of the same class leaves you with new distinct memory allocated for both of the objects.
Jan 26 '11 #4
thanks for replying dlite922,

as you point out, it would not make sense to share attributes (class variables) between objects, because they need to be able to be different from each other.

Static attributes are shared between all objects, that's why they are sometimes called class attributes, rather than instance/object attributes.

But as you may have notice, methods do not change between objects. Let's say the have no state, I was wondering whether they were shared on every instantiation.
You point out the memory cost, but there is also the allocation cpu instuctions cost.
I explain what i mean : PHP is an interpreted language so every piece of code in our scripts is parsed on execution. So every time an instance of a class is created, the class definition must be read and stored in memory, and from that blueprint an instance is created in memory.
That seems fair, if every new instance is of a different class type. But it would be a waste of cpu time to read that class again and again if it were already in memory.

Ex class:
Expand|Select|Wrap|Line Numbers
  1. class A 
  2. {
  3.   public $varA;
  4.   public function methodA()
  5.   {
  6.     //20 lines of code
  7.   }
  8. }
  9.  
  10. class B
  11. {
  12.   public $varB;
  13.   public funciton methodB()
  14.   {
  15.     //20 lines of more code
  16.   }
  17. }
  18.  
let's create some instances of these two classes :
Expand|Select|Wrap|Line Numbers
  1. $a = new A();
  2. $b = new B();
  3. $a2 = new A();
  4.  
So here it seems fair that the PHP parser put class A in memory, then create an instance of that class, by allocating memory for attribure $varA and methodA, and then associating the pointer of that instance to variable $a.
Same thing on line 2 for var $b as class B is not already in memory...
But now the important part, for $a2; class A was already put in memory so let's reuse that (skip parsing class A to put it in memory as we already did) and create an instance of class A. Now that's where my first post question was aimed at : when creating that new instance of class A do we have to allocate memory for methodA again, or there is a shared memory for methods?

Well I can summarize the question : how does an object look like in memory?
Jan 26 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Google Mike | last post by:
I'm using PHP 4.2.2 on RH9 Linux (with Progeny.com updates, in case you're wondering). I was using shared memory (shm* API) a great deal in my web applications because I have 1GB of RAM on this...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
3
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++....
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
11
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I...
14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
5
by: CristianMori | last post by:
Hi there Is there a way in .NET to do shared memories between applications running on the same machine? Thanks
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
5
by: Matias Surdi | last post by:
Suppose I've a process P1, which generates itself a lot of data , for example 2Mb. Then, I've a process P2 which must access P1 shared memory and, probably, modify this data. To accomplish this,...
0
by: Tosh2pointO | last post by:
Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.