Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
On 1 of the newsgroups, someone asked about the pros/cons differences between using forks or threads. Well the newsgroup is c oriented, and I thought I would mention the way that interpreted languages (like Perl) will hold onto memory released by threads. Specifically I mentioned that if a high memory usage thread is run first, then a series of low memory usage threads, the memory of the process will stay at the high memory usage. I used the following script to demonstrate. (I have a 1 gig ram on a linux 2.6.22 kernel). The results were unpredictable, but generally followed the rule that heavy mem use is freed, medium use is half freed, and light use ( <5%) is not freed( but may be on occaision).
#!/usr/bin/perl use warnings; use strict; use threads; my $count = 0; # load a mem intensive thread first, then watch how mem drops # notice the difference in memory reclamation( top) after # the following thread is run with heavy,medium, light mem # on my machine # heavy mem use will skyrocket to 75%, then drop to 2% # medium will jump to 40% then drop to 20% # light use will jump to 5% then stay there # none use will stay at .3 % #my $t = threads->new(\&do_my_thing, $count, 1000000); #light #my $t = threads->new(\&do_my_thing, $count, 4000000); #medium my $t = threads->new(\&do_my_thing, $count, 8000000); #heavy #my $t = threads->new(\&do_my_thing, $count, 1); #none $t->join; select(undef,undef,undef,.1); while (1) { $count++; print "thread $count started\n"; my $t = threads->new(\&do_my_thing, $count, 1 ); $t->join; select(undef,undef,undef,.1); } <>; #wait for keypress to end, to check top sub do_my_thing { my ($val,$cdown) = @_; my %hash; for(0..$cdown){ $hash{$_} = 'a'; } select(undef,undef,undef,.1); print "thread $val ended\n"; }
So it seems that the kernel is deciding how much to free, depending on some internal formula.

The question is: Can we trust top/ps to really report what is going on. Since most threaded scripts may go 10% up occaisionally, if they don't reach the kernel threshold for freeing, is that dead memory, or will the kernel auto-free more (the diff between 5% and .3%) if it needs the memory for other apps.

This would be great news for threaded apps, but I worry about not being able to trust top/ps, or the kernel doing things behind the scenes.

Anyways, this may be a mechanism to reclaim memory in Perl. I would appreciate any thoughts, comments on what is happening.


I'm not really a human, but I play one on earth Remember How Lucky You Are

In reply to OS memory reclamation with threads on linux by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and God said, "Let Newton be!"...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (8)
    As of 2017-10-23 09:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (278 votes). Check out past polls.

      Notices?