Testing for memory leaks

B

Berger, Daniel

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C37D64.007DE79E
Content-Type: text/plain

Hi all,

Being as I write a lot of extensions, I've discovered that it's best to
check for memory leaks. The way I'm doing this now is something like this:

# pseudo code
while 1
call some_method
if Linux or BSD
check /proc/Process.pid/fd count
elsif Solaris
check pfiles Process.pid count
elsif Win32
start Task Manager and watch used file descriptors and memory for
Process.pid
end
sleep 1
end

# Ctrl-C to break out of loop only after at least 10 iterations

Is there a better and/or more streamlined way to do this? Any chance of
adding assert_no_memory_leak to TestUnit, for example?

Regards,

Dan


------_=_NextPart_001_01C37D64.007DE79E
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2655.35">
<TITLE>Testing for memory leaks</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2 FACE=3D"Arial">Hi all,</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Being as I write a lot of extensions, =
I've discovered that it's best to check for memory leaks.&nbsp; The way =
I'm doing this now is something like this:</FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial"># pseudo code</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">while 1</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; call =
some_method</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; if Linux or =
BSD</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
check /proc/Process.pid/fd count</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; elsif =
Solaris</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
check pfiles Process.pid count</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; elsif Win32</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
start Task Manager and watch used file descriptors and memory for =
Process.pid</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; end</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; sleep 1</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">end</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial"># Ctrl-C to break out of loop only =
after at least 10 iterations</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Is there a better and/or more =
streamlined way to do this?&nbsp; Any chance of adding =
assert_no_memory_leak to TestUnit, for example?</FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Regards,</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Dan</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">&nbsp;&nbsp;&nbsp; </FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C37D64.007DE79E--
 
S

Sean O'Dell

Hi all,

Being as I write a lot of extensions, I've discovered that it's best to
check for memory leaks. The way I'm doing this now is something like this:

# pseudo code
while 1
call some_method
if Linux or BSD
check /proc/Process.pid/fd count
elsif Solaris
check pfiles Process.pid count
elsif Win32
start Task Manager and watch used file descriptors and memory for
Process.pid
end
sleep 1
end

# Ctrl-C to break out of loop only after at least 10 iterations

Is there a better and/or more streamlined way to do this? Any chance of
adding assert_no_memory_leak to TestUnit, for example?

The way I have been handling it is I've been launching an infinite loop
that creates instances of my extension and uses it, then calls GC.start
every iteration. I launch it as a background process and then run top
and watch its memory usage. If it hovers at a certain point and doesn't
grow and grow, I assume its safe. To provide a positive, I first
deliberately put in code that should prevent the GC from collecting
something, then run top. Watch it grow and grow. Put the code back to
normal, watch it grow to a certain point and then hover.

Sean O'Dell
 
L

Lothar Scholz

Hello Daniel,

Wednesday, September 17, 2003, 10:38:24 PM, you wrote:

BD> Testing for memory leaksHi all,

BD> Being as I write a lot of extensions, I've discovered that
BD> it's best to check for memory leaks.  The way I'm doing this now
BD> is something like this:

BD> Is there a better and/or more streamlined way to do this? 
BD> Any chance of adding assert_no_memory_leak to TestUnit, for
BD> example?

One of the best ways to detect memory leaks is to add the Boehm GC in
"leak report mode" to your app. This does not find everything but it
should be very easy to recompile with this enabled. Or search source
forge for the zillions of memory debugging libraries.

I'm not sure if ruby does free everything on normal exit. If not you
get some real problems with all this tools.

I would highly recommend to create a debugging ruby library and a
normal ruby library. There are much more things that could be added to
the debugging version at the C core level.
 
N

nobu.nokada

Hi,

At Thu, 18 Sep 2003 06:38:24 +0900,
Being as I write a lot of extensions, I've discovered that it's best to
check for memory leaks. The way I'm doing this now is something like this:

This is the script "memusage.rb" I'm using on Linux. Requires
procfs mounted at /proc.

#! /usr/bin/ruby
module Memory
keys = []
vals = []
IO.foreach("/proc/self/status") do |l|
/^Vm(\w+):\s+(\d+)/ =~ l or next
keys << $1.downcase.intern
vals << $2.to_i
end
Status = Struct.new(*keys)
Status.module_eval do
const_set:)Header, " "+keys.collect {|k| k.to_s.upcase.rjust(6)}.join)
const_set:)Format, "%6d")
end
init = Status.new(*vals)
class Status
@@count = 0
def initialize
@count = @@count += 1
IO.foreach("/proc/self/status") do |l|
/^Vm(\w+):\s+(\d+)/ =~ l or next
self.__send__($1.downcase+"=", $2.to_i)
end
end
def to_s
"%4d: "%@count + collect {|n| Format % n}.join
end
end
puts Status::Header, init
end
if __FILE__ == $0
10.times do
puts Memory::Status.new
GC.start
end
end

Is there a better and/or more streamlined way to do this? Any chance of
adding assert_no_memory_leak to TestUnit, for example?

I don't guess that assertion is easy. Generally, it's
difficult to determin the memory is really leaked or not.
 
A

ahoward

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C37D64.007DE79E
Content-Type: text/plain

Hi all,

Being as I write a lot of extensions, I've discovered that it's best to
check for memory leaks. The way I'm doing this now is something like this:

# pseudo code
while 1
call some_method
if Linux or BSD
check /proc/Process.pid/fd count
elsif Solaris
check pfiles Process.pid count
elsif Win32
start Task Manager and watch used file descriptors and memory for
Process.pid
end
sleep 1
end

# Ctrl-C to break out of loop only after at least 10 iterations

Is there a better and/or more streamlined way to do this? Any chance of
adding assert_no_memory_leak to TestUnit, for example?

not ruby specific, but check out dmalloc and electric fence. they are both
free, easy to use, and work pretty well.

-a
====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: (e-mail address removed)
| Phone: 303-497-7238
| Fax: 303-497-7259
| ~ > ruby -e 'p(%.\x2d\x29..intern)'
====================================
 
S

Simon Strandgaard

I would highly recommend to create a debugging ruby library and a
normal ruby library. There are much more things that could be added to
the debugging version at the C core level.

Great Idea.. Would it be an idea to create a new target
in Ruby's makefile which can build a extension-debug-version of Ruby ?
 
B

Brett H. Williams

Hi all,

Being as I write a lot of extensions, I've discovered that it's best to
check for memory leaks. The way I'm doing this now is something like this:

# pseudo code
while 1
call some_method
if Linux or BSD
check /proc/Process.pid/fd count
elsif Solaris
check pfiles Process.pid count
elsif Win32
start Task Manager and watch used file descriptors and memory for
Process.pid
end
sleep 1
end

# Ctrl-C to break out of loop only after at least 10 iterations

Is there a better and/or more streamlined way to do this? Any chance of
adding assert_no_memory_leak to TestUnit, for example?

If you only care about x86, you might out valgrind. It integrates nicely
with gdb and does a decent job without much effort to get going.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top