[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

how much memory is used by certain variable?

i'm writing script, which should scan a website. it creates http request, posts it, parses returned html and stores visited urls in hash. this script use a damn lot of memory.
is there any way to find out, whether it's because of memory leak due to uncollected garbage, or it's the hash, who is using so much memory? in general, is there any way to estimate at run time the structure of memory, used by script?
0
egor_duda
Asked:
egor_duda
  • 2
  • 2
1 Solution
 
egor_dudaAuthor Commented:
Edited text of question
0
 
b2piCommented:
One of the easiest ways to do this is to use the Storable module, and
then Store the variables, and take a look at the file sizes (note,
it's not necessary to retrieve). Take a look at

perldoc Storable

for usage details. This is a backend way of doing this.  That's
because, simply put, you don't have a memory leak, assuming, of
course, that you're using a properly compiled perl.  You may be using
copious quantities of memory due to any number of reasons, but you
don't have a leak.

Again, you're probably better off looking at your code to try and save
memory.  For instance, you might want to investigate taking memory
abusive functions and forking them off to a separate process, which
will at least return the memory they eat to the OS.

0
 
egor_dudaAuthor Commented:
thanks, it works great! but, alas, it shows that "static" variables (not initialized by "new") are taking less then 1% of memory. so, it seems to me, that the only place, where i can waste memory is such part of code:

while ( ... ) {
  ...
  $h = new HTML::TreeBuilder ;
  ...
}

it creates new object on each iteration. as far, as i understand, the previous instance should be "garbage collected" at the moment of assignment new value to $h. is it so, or i should explicitly free previously allocated memory?
0
 
b2piCommented:
It actually depends on where you're declaring h.  It won't be garbage
collected until h is either reassigned or has gone out of scope. Even
then, if anything else 'points' to it, it still won't be garbage
collected.

BTW, I've no idea offhand how you _can_ free previously allocated
memory.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now