Ruby Programming

  • Thread starter Tridib Bandopadhyay
  • Start date
T

Tridib Bandopadhyay

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.

Regards

Tridib Bandopadhyay
 
J

Jeremy Bopp

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.

Creating any object requires memory allocation of some sort. Are you
actually asking about performing memory allocation as part of a C-based
Ruby extension?

-Jeremy
 
A

Aaron Turner

BEGIN CODE:
#/usr/bin/env ruby
myvar =3D 1
END OF CODE

Easy huh?

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.



--=20
Aaron Turner
http://synfin.net/=A0 =A0 =A0 =A0=A0 Twitter: @synfinatic
http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix & Win=
dows
Those who would give up essential Liberty, to purchase a little temporary
Safety, deserve neither Liberty nor Safety.
=A0 =A0 -- Benjamin Franklin
"carpe diem quam minimum credula postero"
 
T

Tridib Bandopadhyay

Hello

I need to do this...

The Gc is called at the runtime in Ruby. So I try to code a program
which requires memory allocation and Garbage collector.

Now the scenario is such that--- In the gc.c file if I write some print
statement within any function.So when the gc comes for working, Will it
show me the message
which i tried to print out.?

Regards

Tridib
 
A

Alex Gutteridge

Hello

I need to do this...

The Gc is called at the runtime in Ruby. So I try to code a program
which requires memory allocation and Garbage collector.

Now the scenario is such that--- In the gc.c file if I write some print
statement within any function.So when the gc comes for working, Will it
show me the message
which i tried to print out.?

Regards

Tridib

Sure. There's also the GC_NOTIFY and GC_DEBUG defines in gc.c and
GC::profiler which you can call from Ruby. As people have already pointed
out *any* Ruby program will require memory allocation and hence GC.
 
T

Tridib Bandopadhyay

I have done a coding
The code:-

# Class names must be capitalized. Technically, it's a constant.
class Fred

# The initialize method is the constructor. The @val is
# an object value.
def initialize(v)
@val = v
end

# Set it and get it.
def set(v)
@val = v
end

def get
return @val
end
end

# Objects are created by the new method of the class object.
a = Fred.new(10)
b = Fred.new(22)

print "A: ", a.get, " ", b.get,"\n";
b.set(34)
print "B: ", a.get, " ", b.get,"\n";

# Ruby classes are always unfinished works. This does not
# re-define Fred, it adds more stuff to it.
class Fred
def inc
@val += 1
end
end

a.inc
b.inc
print "C: ", a.get, " ", b.get,"\n";

# Objects may have methods all to themselves.
def b.dec
@val -= 1
end

begin
b.dec
a.dec
rescue StandardError => msg
print "Error: ", msg, "\n"
end

print "D: ", a.get, " ", b.get,"\n";
_________________________

Is there any syntax to see how much memory the code is using?.

Regards

Tridib
 
T

Tridib Bandopadhyay

I am trying for the code

#/usr/bin/env ruby
myvar = 1

And also made some print statements in gc.c file functions (malloc,
calloc,free, gc_diable, gc_enable) etc.

But when I run the program I am not able to see the print statement.

Secondly, can any one say me that when the gc is called which is the
main function that starts running.

Regards

Tridib
 
T

Tridib Bandopadhyay

Alex Gutteridge wrote in post #962544:
Sure. There's also the GC_NOTIFY and GC_DEBUG defines in gc.c and
GC::profiler which you can call from Ruby. As people have already
pointed
out *any* Ruby program will require memory allocation and hence GC.

Can you tell me how to call those two?...And do I need to write some
print statement within those one.??

Regards
 
A

Alex Gutteridge

Alex Gutteridge wrote in post #962544:

Can you tell me how to call those two?...And do I need to write some
print statement within those one.??

Regards

There doesn't seem to be any mystery here, just search for GC_NOTIFY in
gc.c and set it to true rather than false. E.g.

#define GC_NOTIFY 0

becomes

#define GC_NOTIFY 1

That change alone prints "start garbage_collect()" whenever
garbage_collect() is run:

GUTTEA$ ./ruby -e 'GC.start'
start garbage_collect()
end garbage_collect()

Defining RUBY_MARK_FREE_DEBUG in gc.h gives lots more info as well:

GUTTEA$ ./ruby -e 'GC.start'
start garbage_collect()
mark: -> iseq (0x100624900)
require @ <internal:lib/rubygems/custom_require>
mark: -> iseq (0x100624cb0)
rescue in require @ <internal:lib/rubygems/custom_require>
mark: <- iseq (0x100624cb0)
mark: <- iseq (0x100624900)
mark: -> iseq (0x100612b90)
gem @ <internal:gem_prelude>
mark: <- iseq (0x100612b90)
mark: -> iseq (0x10060c950)

[.......]

I don't see any problem with adding your own debug statements as well if
you want:

static int
garbage_collect(rb_objspace_t *objspace)
{
struct gc_list *list;
rb_thread_t *th = GET_THREAD();
INIT_GC_PROF_PARAMS;

if (GC_NOTIFY) printf("start garbage_collect()\n");

printf("WoopWoop\n");

[....]

GUTTEA$ ./ruby -e 'GC.start'
start garbage_collect()
WoopWoop

[....]
 
A

Alex Gutteridge

@Alex Gutteridge

I am using Ruby 1.8.1

And I don't have that GC_NOTIFY function

What to do??..

I have attached my gc.c file...Can you make the correction with
notifying me where you did it?/

Attachments:
http://www.ruby-forum.com/attachment/5489/gc.c

No, sorry. Your version is very out of date (~2003 I think!?!). You'll get
far more help from people if you use a more recent version.
 
T

Tridib Bandopadhyay

Alex Gutteridge wrote in post #965059:
No, sorry. Your version is very out of date (~2003 I think!?!). You'll
get
far more help from people if you use a more recent version.

But I am running on CentOS which supports this Ruby version only..I
tried to compiler Ruby 1.9 but it was not working..

Can change anything within this gc.c file?

Regards
Tridib
 
B

Brian Candler

Tridib Bandopadhyay wrote in post #965077:
But I am running on CentOS which supports this Ruby version only..I
tried to compiler Ruby 1.9 but it was not working..

Compile 1.8.6 or 1.8.7, it will be fine. (Assuming you've got a working
C compiler of course). Or use rvm which takes care of most of this for
you.

There are also ruby 1.8.5 RPM packages for CentOS 4 in the Testing
repository:
http://dev.centos.org/centos/4/testing/i386/RPMS/

Regards,

Brian.
 
T

Tridib Bandopadhyay

Brian Candler wrote in post #965078:
Tridib Bandopadhyay wrote in post #965077:

Compile 1.8.6 or 1.8.7, it will be fine. (Assuming you've got a working
C compiler of course). Or use rvm which takes care of most of this for
you.

There are also ruby 1.8.5 RPM packages for CentOS 4 in the Testing
repository:
http://dev.centos.org/centos/4/testing/i386/RPMS/

Regards,

Brian.

Okay I Have downloaded the Ruby 1.8.6

Can any one say me where is the GC_NOTIFY function in gc.c file?..May be
at which line...

Regards
Tridib
 
P

Phillip Gawlowski

Can any one say me where is the GC_NOTIFY function in gc.c file?..May be
at which line...

Use an editor with a search function?

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
T

Tridib Bandopadhyay

Phillip Gawlowski wrote in post #965134:
Use an editor with a search function?

There no GC_NOTIFY function under gc.c file in Riby 1.8.6 also..

Any other options of printing out..

Regards
 
T

Tridib Bandopadhyay

There is neither of this functions in Ruby 1.8.6 gc.c file also

The functions are:-

GC_NOTIFY ; GC_DEBUG ; GC::profiler

Even if I am trying to print some of my own debug print statement in the
function VALUE rb_newobj()

but its not showing my print statement

What to change in so that I can know when the Garbage Collector is
working.

Regards

Tridib
 
T

Tridib Bandopadhyay

Tridib Bandopadhyay wrote in post #965573:
There is neither of this functions in Ruby 1.8.6 gc.c file also

The functions are:-

GC_NOTIFY ; GC_DEBUG ; GC::profiler

Even if I am trying to print some of my own debug print statement in the
function VALUE rb_newobj()

but its not showing my print statement

What to change in so that I can know when the Garbage Collector is
working.

Regards

Tridib

Any One to Help me in this issue>.??..Its Urgent,

Regards

Tridib
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top