CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
garbage collection

 



psu1991
stranger

Jul 24, 2001, 2:22 PM

Post #1 of 4 (1667 views)
garbage collection Can't Post

$a = 123
$b = \$a
$c = $b
What is the reference count of $a? I think it should be
3, but want to verify with someone that may know for sure.



mhx
Enthusiast

Jul 24, 2001, 10:36 PM

Post #2 of 4 (1656 views)
Re: garbage collection [In reply to] Can't Post

It is 3, at least with my HP-UX build of Perl 5.6.0. I'm not sure if there's an easier way to get the value of the reference counter, but mine isn't too hard anyway. (Took me only some minutes.)

If you have a look at the perlguts manpage, there's a section Reference Counts and Mortality. There's also an internal routine to retrieve the reference counter value of an SV called SvREFCNT. With this knowledge, one can easily write a small XSUB with the following code:

Code
MODULE = RefCount		PACKAGE = RefCount		 

int
GetRefCount( sv )
SV *sv
CODE:
RETVAL = SvREFCNT( sv );
OUTPUT:
RETVAL

You can then use this XSUB from some Perl code like this:

Code
#!/bin/perl -w 
use RefCount;

$a = 123;
$b = \$a;
$c = $b;
printf "RefCount = %d\n", RefCount::GetRefCount( $a );

And in this case, it will print RefCount = 3.

There are also some interesting resources besides the perlguts manpage on the web. There is for example an article called Perl Guts Illustrated that graphically illustrates Perl's internal data types. Also, there's interesting reply by Nathan Torkington concerning circular references and the related problems.

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



psu1991
stranger

Jul 25, 2001, 8:06 AM

Post #3 of 4 (1652 views)
Re: garbage collection [In reply to] Can't Post

Very cool stuff! thanks.
-Jason



mhx
Enthusiast

Aug 3, 2001, 8:06 AM

Post #4 of 4 (1633 views)
Re: garbage collection [In reply to] Can't Post

Hehe, it's even easier to get the reference count of a variable in Perl than through writing an XS yourself. At least if one has a look at the standard Perl modules... Wink

Code
use Devel::Peek; 
$a = 123;
$b = \$a;
$c = $b;
Dump $a;

That's it. It prints something similar to

Code
SV = IV(0x17637c4) at 0x1765374 
REFCNT = 3
FLAGS = (IOK,pIOK,IsUV)
UV = 123

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives