Finding the size of a rather complicated hash-structure

F

Fred

Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to find
the size of. Size in consumed bytes of memory. Just because I'm curious.
And I've done the obligatory searches for finding sizes of stuff, and I'm
not much the wiser. Exept I've come to understand that there isn't any
bult-in functionality to do this for such structures.

So here I am then. Needing help!

My hash-structure looks something like this:

$tree = {
<key> => {
'name' => <str>,
'file' => <str>,
'architectures' => {
<key> => {
'name' => <str>,
'file' => <str>,
'components' => {
<key> = {
'name' => <str>,
'lib' => <str>
}
}
}
}
}
};

Actually it's much bigger, but this gives you a general idea of what it's
like. For each anonymous hash, the '<key>' have several elements,
typically around a hundred in $tree, one or two in
$tree->{<key>}->{'architectures'} and none or a couple in
$tree->{<key>}->{'architectures'}->{<key>}->{'components'}.

I suppose I could loop through tree, pushing name and file to an array,
looping through the architectures for each element in tree, and pushing
name and file to the same array, and doing the same with the components.
And then just flattening the array and use length(). But...that's kinda
dirty.....

(Not that I don't like my gals dirty...and perl's my bitch...so I guess I
might as well go for it...hehe! :-D )

But anyway...any good suggestions on how I can find out how many bytes of
memory my Frankenstein-hash is consuming, without writing a whole program
to find it? Or doing it the 'dirty' way?
 
B

Ben Morrow

Quoth Fred said:
Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to find
the size of. Size in consumed bytes of memory. Just because I'm curious.
And I've done the obligatory searches for finding sizes of stuff, and I'm
not much the wiser. Exept I've come to understand that there isn't any
bult-in functionality to do this for such structures.
Devel::Size

I suppose I could loop through tree, pushing name and file to an array,
looping through the architectures for each element in tree, and pushing
name and file to the same array, and doing the same with the components.
And then just flattening the array and use length(). But...that's kinda
dirty.....

Also completely wrong. Perl data structures have quite a lot of overhead.

Ben
 
A

Anno Siegel

Fred said:
Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to find
the size of. Size in consumed bytes of memory. Just because I'm curious.
And I've done the obligatory searches for finding sizes of stuff, and I'm
not much the wiser. Exept I've come to understand that there isn't any
bult-in functionality to do this for such structures.

That is because the actual data structures perl uses depend on too
many things. It's not only the Perl version and the platform, but
also which compiler was used to build perl and possibly with what
compile-time options.
So here I am then. Needing help!

My hash-structure looks something like this:

Whatever it is, if you were to measure it exactly you'd get wildly
differing results depending on circumstances that will be hard to
control.

[...]
I suppose I could loop through tree, pushing name and file to an array,
looping through the architectures for each element in tree, and pushing
name and file to the same array, and doing the same with the components.
And then just flattening the array and use length(). But...that's kinda
dirty.....

Use one of the linearizing modules to do that for you, Data::Dumper,
Storable or the like. Use a non-compressing mode, then the length
of the resulting string should give reasonable comparisons between
similar data structures. You will not catch any overhead Perl uses
to store the structure.

Anno
 
P

Peter Wyzl

Fred said:
Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to find
the size of. Size in consumed bytes of memory. Just because I'm curious.
And I've done the obligatory searches for finding sizes of stuff, and I'm
not much the wiser. Exept I've come to understand that there isn't any
bult-in functionality to do this for such structures.

So here I am then. Needing help!

My hash-structure looks something like this:

$tree = {
<key> => {
'name' => <str>,
'file' => <str>,
'architectures' => {
<key> => {
'name' => <str>,
'file' => <str>,
'components' => {
<key> = {
'name' => <str>,
'lib' => <str>
}
}
}
}
}
};

Use Storable

Write it to disk and look at the size of the file you create...
 
K

ko

Fred said:
Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to
find the size of. Size in consumed bytes of memory. Just because I'm
curious. And I've done the obligatory searches for finding sizes of
stuff, and I'm not much the wiser. Exept I've come to understand that
there isn't any bult-in functionality to do this for such structures.

So here I am then. Needing help!

My hash-structure looks something like this:

$tree = {
<key> => {
'name' => <str>,
'file' => <str>,
'architectures' => {
<key> => {
'name' => <str>,
'file' => <str>,
'components' => {
<key> = {
'name' => <str>,
'lib' => <str>
}
}
}
}
}
};

Have a look at Devel::Size

HTH - keith
 
C

ctcgag

Fred said:
Hi group

Ok, so I've got a rather complicated hash-structure that I'd like to find
the size of. Size in consumed bytes of memory. Just because I'm curious.
And I've done the obligatory searches for finding sizes of stuff, and I'm
not much the wiser. Exept I've come to understand that there isn't any
bult-in functionality to do this for such structures.


The Tie::Cache module has code that tries to do this, but as of version
0.17, it was wildly inaccurate for nested structures with numbers or small
strings as the final data elements. I tweaked it some to make it better,
but was never happy enough with the changes to submit them to the author as
an improvement. If your interested, I could go back and look into it
again.


Xho
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top