Question on having the hash not butt plug my computers memory

  • Thread starter grocery_stocker
  • Start date
G

grocery_stocker

Given the following:

#!/usr/bin/perl -w

package Person;

sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = {
NAME => undef,
AGE => undef,
PEERS => []
};

my $closure = sub {
my $field = shift;
if (@_) { $self->{$field} = shift }
return $self->{$field};
};

bless($closure, $class);
return $closure;
}

sub name { &{ $_[0] }("NAME", @_[ 1 .. $#_ ] ) }

package main;

$him = Person->new();
$him2 = Person->new();
$him3 = Person->new();

$him->name("my ex girlfried");
$him2->name("is a");
$him3->name("whore");

print $him->name, "\n";
print $him2->name, "\n";
print $him3->name, "\n";

This presumably creates 3 hashes. If I modified this to create say like
30,000 hashes, this could possibly blow.

Okay, if modified the code to have something like:
use Symbol;
$obj = Symbol::gensym();

vs

my $self = {
NAME => undef,
AGE => undef,
PEERS => []
};

Would I still be creating more memory each time I invoked a new
instance of the object?
 
B

Brian McCauley

Given the following:

[ object implementation ]
Okay, if modified the code to have something like:

[ slightly different implementation ]
Would I still be creating more memory each time I invoked a new
instance of the object?

Er, creating instanced of objects that store data will always occupy
memory.

What was your real question?
 
G

grocery_stocker

Brian said:
Given the following:

[ object implementation ]
Okay, if modified the code to have something like:

[ slightly different implementation ]
Would I still be creating more memory each time I invoked a new
instance of the object?

Er, creating instanced of objects that store data will always occupy
memory.

What was your real question?

If I used gensym (via the Symbol package), then does each instance of
the object take up more memory. Say I create one object that takes up 3
things of memory via gensym. If I create another object via gensym,
would it create another three things of memory on top of the other
three things of memory (for a total of 6 things) like what an anonymous
hash would.
 
B

Brian McCauley

[ object implementation ]
Okay, if modified the code to have something like:
[ slightly different implementation ]
Would I still be creating more memory each time I invoked a new
instance of the object?
Er, creating instanced of objects that store data will always occupy
memory.
What was your real question?
If I used gensym (via the Symbol package), then does each instance of
the object take up more memory. Say I create one object that takes up 3
things of memory via gensym. If I create another object via gensym,
would it create another three things of memory on top of the other
three things of memory (for a total of 6 things) like what an anonymous
hash would.

Er, yes. All other things being equal, representing your object as a
GLOB containing a given HASH will occupy more memory than just
representing your object as the HASH directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top