[ Pobierz całość w formacie PDF ] .5 Symbolic Next: 1.7 References in OtherData References and AnonymousReferences LanguagesStorage1.6 A View of the InternalsLet us now take a look inside Perl to understand how it manages memory.You can safely skip thissection without loss of continuity.A variable logically represents a binding between a name and a value, as Figure 1.3 illustrates.[6][6] This is true whether the variable is global, dynamically scoped (using local()), orlexically scoped (using my()).More details are given in Chapter 3.Figure 1.3: A variable is a name and value pairAn array or a hash is not just a collection of numbers or strings.It is a collection of scalar values, andthis distinction is important, as Figure 1.4 illustrates.Figure 1.4: An array value is a collection of scalar valuesEach box in Figure 1.4 represents a distinct value.An array has one value that represents the collection ofscalar values.Each element of the array is a distinct scalar value.This is analogous to a pride of lionsbeing treated as a single entity (which is why we refer to it in the singular) that has properties distinctfrom those of the individual lion.Notice also that while a name always points to a value, a value doesn't always have to be pointed to by aname, as the array elements in Figure 1.4 or anonymous arrays and hashes exemplify.1.6.1 Reference CountsTo support painless and transparent memory management, Perl maintains a reference count for everyvalue, whether it is directly pointed to by a name or not.Let's add this piece of information to our earlierview.Refer to Figure 1.5.Figure 1.5: Adding reference counts to all valuesAs you can see, the reference count represents the number of arrows pointing to the value part of avariable.Because there is always an arrow from the name to its value, the variable's reference count is atleast 1.When you obtain a reference to a variable, the corresponding value's reference count isincremented.It is important to stress the point that even though we would like to think of $ra as pointing to $a, it reallypoints to $a's value.In fact, $ra does not even know whether the value it is pointing to has acorresponding entry in the symbol table.The value of the reference variable is the address of anotherscalar value, which does not change even if $a's value changes.Perl automatically deletes a value when its reference count drops to zero.When variables (named values)go out of scope, the binding between the name and the value is removed, resulting in the value'sreference count being decremented.In the typical case in which this count is 1, the value is deleted (orgarbage collected ).[7][7] For efficiency, Perl doesn't actually delete it; it just sends it to its own free pool andreuses it when you need a new value.It is logically deleted, nevertheless.The reference counting technique is sometimes referred to as "poor man's garbage collection," in contrastto much more sophisticated techniques used by environments such as LISP, Java, and Smalltalk (thoughthe early versions of Smalltalk used reference counting).The problem is that reference counts take upspace, which adds up if you consider that every piece of data in your application has an extra integerassociated with it.Then there is also the problem of circular references.The simplest case is this:$a = \$a;This is a classic case of narcissism.$a's reference count indicates that something is pointing to it, so itwill never get freed.A more practical case of circular references is that of network graphs (each nodekeeps track of each of its neighbors) or ring buffers (where the last element points to the first one).Modern garbage collection algorithms implemented in Java and Smalltalk can detect circular referencesand deallocate the entire circular structure if none of the elements are reachable from other variables.On the other hand, reference counting is simple to understand and implement and makes it easy tointegrate Perl with C or C++ code.Please refer to item 2 in the Section 1.8, "Resources" section at theend of the chapter for a comprehensive treatment of garbage collection techniques.Note that while symbolic references allow you to access variables in an indirect way, no actual referencevariables are created.In other words, the reference count of a symbolically accessed variable is notmodified.Hence symbolic references are also called soft references, in contrast to hard references, whichactually allocate storage to keep track of the indirection.This is similar to the concept of soft versus hard links in the Unix filesystem.The i-node of a file has itsreference count incremented every time someone creates a hard link to that file, so you can't really deletethe file's contents until its reference count goes down to zero.A symbolic link, on the other hand, storesonly the name of the file and can point to a nonexistent file; you'll never know until you try to open thefile using the symbolic link.1.6
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plhanula1950.keep.pl
|