CS 123 - Memory model for pictureInAPicture method

Below are three illustrations of memory maps for the following code fragment.

   > Picture x = new Picture("c:/cs123/mediasources/pictures/butterfly1.jpg")
   > Picture y = new Picture("c:/cs123/mediasources/pictures/turtle.jpg")
   > x.pictureInAPicture(300, 150, y)
   > x.show()
The first memory map shows what memory looks like just before the pictureInAPicture() method is called, that is, just after executing the first two lines that declare the two reference variables and create the two objects.
The second memory map shows what memory looks like just before the pictureInAPicture() method returns. Notice how the interaction reference variable x and the method local variable this both refer to the same object (the object that initially held the data from the butterfly1.jpg file). Similarly, the interaction reference variable y and the method local variable p2 both refer to the same object. As the pictureInAPicture() method executes, it modifies (mutates) the data in the object referred to by this (and by x). So when the method returns, the interaction reference variable x is left referring to the modified picture object.
The pictureInAPicture frame should really hold ten local variables, even though the above illustration shows only two of them. The ten local variables are
   this
   x
   y
   p2
   sourceX
   targetX
   sourceY
   targetY
   sourcePixel
   targetPixel
After the pictureInAPicture() method returns, the pictureInAPicture frame (and all ten its local variables) disappear from memory. The following memory maps illustrates memory after the return of the pictureInAPicture() method. Compared to the first illustration above, we see that the object referenced by x has mutated.


Return to the CS 123 lecture page.


compliments and criticisms