Monitoring total memory usage?

A

Andrew Backer

Hello,

I need to monitor the memory usage of a ruby script, and I would like
to see if I can do it with within the program itself, or from a monitor
script also in ruby. Is there a way to do this? Using school as an
excuse to learn ruby, and this is one part of it.

just thought i'd throw my 2c in
1) no true reference parameters is a pain (how do i write a swap!)
2) a,b = b,a is the coolest thing ( oh, thats how! )
3) array wrapping can be an error and is not to be allowed sometimes
4) (can I turn it off?)
5) it is so easy to extend built in classes! ( added an array.resize()
)
6) why is there no serious formatting support built into the objects? I
guess I'm spoiled by C# now.

Aside from the usual "damn thing doesn't work" gripes, it's great.

Thanks,
Andrew Backer
 
J

Joe Van Dyk

Hello,

I need to monitor the memory usage of a ruby script, and I would like
to see if I can do it with within the program itself, or from a monitor
script also in ruby. Is there a way to do this? Using school as an
excuse to learn ruby, and this is one part of it.

just thought i'd throw my 2c in
1) no true reference parameters is a pain (how do i write a swap!)
2) a,b =3D b,a is the coolest thing ( oh, thats how! )
3) array wrapping can be an error and is not to be allowed sometimes
4) (can I turn it off?)

What the heck do you mean by 'array wrapping'?
5) it is so easy to extend built in classes! ( added an array.resize() )
6) why is there no serious formatting support built into the objects? I
guess I'm spoiled by C# now.

What do you mean by 'formatting support'?
 
H

Hal Fulton

Andrew said:
Hello,

I need to monitor the memory usage of a ruby script, and I would like
to see if I can do it with within the program itself, or from a monitor
script also in ruby. Is there a way to do this? Using school as an
excuse to learn ruby, and this is one part of it.

Hmm. The GC must know something about this, but I don't think it
exposes it. Maybe it should?

Or maybe it's possible to make a system call or something? Of course,
a little Heisenberg gets thrown in...

Failing that, you *might* be able to traverse object space somehow
and add up the memory usage. I wouldn't want to do it.
just thought i'd throw my 2c in
1) no true reference parameters is a pain (how do i write a swap!)
2) a,b = b,a is the coolest thing ( oh, thats how! )

:) Yes. You can also pass in arrays (whose contents can be changed). I
don't recommend using arrays or any other such thing as though it were
a real reference parameter, though. Usually when I (start to) want
ref params, I just return multiple values from the method instead.
3) array wrapping can be an error and is not to be allowed sometimes
4) (can I turn it off?)
Wrapping?

5) it is so easy to extend built in classes! ( added an array.resize()

Exercise normal caution when using this feature. :)
6) why is there no serious formatting support built into the objects? I
guess I'm spoiled by C# now.

Formatting? You mean like in output?

Do you know about:

p object

and

require 'pp'
pp object
?


Cheers,
Hal
 
A

Andrew Backer

Wow, quick responses ;)

No, i don't know about PP. I am just accustomed to having serious
output formatting control in fortran, c & derivitives, etc. for
printing ints, floats, hex, padding, ... Its probably here, I just
don't know where it is.

I just extend the class for each individual program. I wrote the
resize so I don't have to worry about what arrays I pass into the
function. resize(n,def) just .clear's it and stuffs a bunch of 'def'
in to up to size n. I haven't yet writting large ruby programs, so no
experience worrying about things.

Array *index* wrapping is useful in some cases (fibonacci numbers with
no extra assignments). However, most of the time when I accidentially
go past the lower bounds I would like to know since a[x-1] wasn't
intended to set the value at a[9], but since x was zero it did.

nums = [1,2,3] # nums[-1] = 3 mums[2] = 3, nums[-2] = 2, etc

def calc_fib(n)
a = [0,1,0]
for i in 2..n
k = i%3
a[k] = a[k-1] + a[k-2]
end
return a[n%3]
end
 
A

Ara.T.Howard

Wow, quick responses ;)

No, i don't know about PP. I am just accustomed to having serious
output formatting control in fortran, c & derivitives, etc. for
printing ints, floats, hex, padding, ... Its probably here, I just
don't know where it is.

harp:~ > irb
irb(main):001:0> printf "%s\n", "foobar"
foobar

irb(main):002:0> printf "%b\n", 42
101010

irb(main):004:0> puts( '|' << ('%s' % 'foobar').center(42) << '|')
| foobar |

all the printf modifiers, and more, are there. class string also has many
formatting features.

I just extend the class for each individual program. I wrote the resize so
I don't have to worry about what arrays I pass into the function.
resize(n,def) just .clear's it and stuffs a bunch of 'def' in to up to size
n. I haven't yet writting large ruby programs, so no experience worrying
about things.

Array *index* wrapping is useful in some cases (fibonacci numbers with
no extra assignments). However, most of the time when I accidentially
go past the lower bounds I would like to know since a[x-1] wasn't
intended to set the value at a[9], but since x was zero it did.

nums = [1,2,3] # nums[-1] = 3 mums[2] = 3, nums[-2] = 2, etc

def calc_fib(n)
a = [0,1,0]
for i in 2..n
k = i%3
a[k] = a[k-1] + a[k-2]
end
return a[n%3]
end

harp:~ > cat a.rb
class Array
alias_method '__idx__', '[]'
def [] *a, &b
n = a.first
raise RangeError, "negative index <#{ n }>" if Numeric === n and n < 0
send '__idx__', *a, &b
end
end

a = []
a << 42
p a[0]
p a[0, 1]
p a[0 .. 0]
p a[-1]


harp:~ > ruby a.rb
42
[42]
[42]
a.rb:6:in `[]': negative index <-1> (RangeError)
from a.rb:16

hth.


-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
J

Joe Van Dyk

Wow, quick responses ;)

No, i don't know about PP. I am just accustomed to having serious
output formatting control in fortran, c & derivitives, etc. for
printing ints, floats, hex, padding, ... Its probably here, I just
don't know where it is.

There's printf and friends in Ruby. Check http://www.ruby-doc.org or
the 'Programming Ruby' book by the Pragmatic Programmers.
I just extend the class for each individual program. I wrote the
resize so I don't have to worry about what arrays I pass into the
function. resize(n,def) just .clear's it and stuffs a bunch of 'def'
in to up to size n. I haven't yet writting large ruby programs, so no
experience worrying about things.

I still have no idea why you want to manually resize an Array.
 
N

Nikolai Weibull


Thanks for remembering! Actually, I=E2=80=99m about to release a new ver=
sion of
it. It=E2=80=99s gone through a complete rewrite and is a lot faster, a =
lot
more true to the specification, and a lot less documented :-(. I=E2=80=99=
m
trying to find time to write some documentation, but I never seem to be
able to sit down and finish it...,
nikolai

--=20
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
 
A

Andrew Backer

Here's why I wanted to resize an array : I am translating a function
which has 3 output variables, two of which are arrays. It would be
nice to pass in the array, and have the function resize it to the
appropriate size, keeping the semantics of the function as close to the
original as possible. Just like you standard array list. It is
possible to grow an array (with <<), but not to just 'resize to 7',
hence the function which just clears and stuffs with with << 0 .

Turned out I just used mutliple return values ( arr1, arr2, err =
myFunc( input ) ) insteand of the other way, and it's much nicer now.
I don't have to worry about it, I just make a new array in the function
and return it, so my routine has only one input variable.

As for the fancy stuff done by Ara, I think I am staring to get it
after I look at it. Not sure exactly what is going on, except that "a"
is the parameter list, right? and... &b is the block passed... so it
checks that A isn't negative. If it isn't, it forwards the call to the
origional index function. I assume the particular syntax is just the
way the __idx__ routine is defined in the first place.

I just don't qite get the alias statement, and the exact mechanics of
what it is covering up/exposing/ etc..

Thanks for all the help!

- Andrew

p.s. I should also mention this thread no longer has anything to do
with the title. Also, that apparently 'memory usage' in this prof's
mind means 'compiled executable size'. Shucks.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top