[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to understand why object is not collected?

Victor 'Zverok' Shepelev

10/28/2006 6:26:00 PM

Hi all.

Here's a problem.

I have some object of MyClass with complex "freeing" logic, and I want to
test this logic.
I do:

---------
span = .... #somehow created object

ObjectSpace.define_finalizer(span, proc{p "gc ok"}) #define test finalizer

span.detach #remove object from all of my internal caches, no references
to object should exist

span = nil #remove the last reference to object

GC.start #<===HERE the object must be collected and finalized

p "end of code"
---------

I expect:
---
"gc ok"
"end of code"
---

I receive:
---
"end of code"
"gc ok"
---

OK, I suppose there are some references to the object still exists. I want
to know, at least, what number of references still exists (in ideal, I'd
want to know, where they are).
How I can? (except of harcode hacking GC's C code)

Ruby 1.9 (downloaded a week ago), Windows.

Thanks.

V.


11 Answers

Joel VanderWerf

10/28/2006 7:52:00 PM

0

Victor "Zverok" Shepelev wrote:
> Hi all.
>
> Here's a problem.
>
> I have some object of MyClass with complex "freeing" logic, and I want to
> test this logic.
> I do:
>
> ---------
> span = .... #somehow created object
>
> ObjectSpace.define_finalizer(span, proc{p "gc ok"}) #define test finalizer
>
> span.detach #remove object from all of my internal caches, no references
> to object should exist
>
> span = nil #remove the last reference to object
>
> GC.start #<===HERE the object must be collected and finalized

This is probably a situation where GC is being conservative. A simple
example:

class Foo; end

foo = Foo.new
foo = nil

GC.start

p ObjectSpace.each_object(Foo) {} # ==> 1

Probably this means that the Foo instance pointer is still visible
somewhere in the local frame.

But that doesn't mean that there is a leak:

class Foo; end

10.times do
Foo.new
end

GC.start

p ObjectSpace.each_object(Foo) {} # ==> 1

In my experience, it's hard to write precise unit tests for GC behavior,
and you end up with some fuzziness. The important property of GC is
really the asymptotic behavior.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Victor 'Zverok' Shepelev

10/28/2006 8:00:00 PM

0

From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu]
Sent: Saturday, October 28, 2006 10:52 PM
>This is probably a situation where GC is being conservative. A simple
>example:
>
>class Foo; end
>
>foo = Foo.new
>foo = nil
>
>GC.start
>
>p ObjectSpace.each_object(Foo) {} # ==> 1

OK, I've got it.

>In my experience, it's hard to write precise unit tests for GC behavior,
>and you end up with some fuzziness. The important property of GC is
>really the asymptotic behavior.

Just for now, I need no precise unit test. I just want to know, would the
objects of MyClass collected in principle. Can I achieve the goal somehow?

V.


Eero Saynatkari

10/28/2006 8:20:00 PM

0

On 2006.10.29 03:26, Victor Zverok Shepelev wrote:
> Hi all.
>
> Here's a problem.
>
> I have some object of MyClass with complex "freeing" logic, and I want to
> test this logic.
> <snip />

Hard to say what is going wrong but the solution is simple. Whenever
you have a resource that must be released, do it explicitly. The way
this is usually implemented is either:

f = File.open 'foo'
puts f.read
f.close

Or, more conveniently

File.open('foo') {|f| puts f.read}

The block-form uses ensure to make sure the file handle gets closed.

Victor 'Zverok' Shepelev

10/28/2006 8:24:00 PM

0

From: Eero Saynatkari [mailto:ruby-ml@kittensoft.org]
Sent: Saturday, October 28, 2006 11:20 PM
>On 2006.10.29 03:26, Victor Zverok Shepelev wrote:
>> Hi all.
>>
>> Here's a problem.
>>
>> I have some object of MyClass with complex "freeing" logic, and I want to
>> test this logic.
>> <snip />
>
>Hard to say what is going wrong but the solution is simple. Whenever
>you have a resource that must be released, do it explicitly. The way
>this is usually implemented is either:
>
> f = File.open 'foo'
> puts f.read
> f.close
>
>Or, more conveniently
>
> File.open('foo') {|f| puts f.read}
>
>The block-form uses ensure to make sure the file handle gets closed.

I know this (I was C++ guy foe many years, so RAII is my breath), but can't
use.
My objects are HTML DOM elements and I want to be sure they are GC'd when
detached from DOM and have no other references.

V.


Joel VanderWerf

10/28/2006 8:26:00 PM

0

Victor "Zverok" Shepelev wrote:
> From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu]
> Sent: Saturday, October 28, 2006 10:52 PM
>> This is probably a situation where GC is being conservative. A simple
>> example:
>>
>> class Foo; end
>>
>> foo = Foo.new
>> foo = nil
>>
>> GC.start
>>
>> p ObjectSpace.each_object(Foo) {} # ==> 1
>
> OK, I've got it.
>
>> In my experience, it's hard to write precise unit tests for GC behavior,
>> and you end up with some fuzziness. The important property of GC is
>> really the asymptotic behavior.
>
> Just for now, I need no precise unit test. I just want to know, would the
> objects of MyClass collected in principle. Can I achieve the goal somehow?

What about creating a lot of them in a loop, without keeping references?
Then I would expect GC to leave at most one instance.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Victor 'Zverok' Shepelev

10/28/2006 8:39:00 PM

0

From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu]
Sent: Saturday, October 28, 2006 11:26 PM
>Victor "Zverok" Shepelev wrote:
>> From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu]
>> Sent: Saturday, October 28, 2006 10:52 PM
>>> This is probably a situation where GC is being conservative. A simple
>>> example:
>>>
>>> class Foo; end
>>>
>>> foo = Foo.new
>>> foo = nil
>>>
>>> GC.start
>>>
>>> p ObjectSpace.each_object(Foo) {} # ==> 1
>>
>> OK, I've got it.
>>
>>> In my experience, it's hard to write precise unit tests for GC behavior,
>>> and you end up with some fuzziness. The important property of GC is
>>> really the asymptotic behavior.
>>
>> Just for now, I need no precise unit test. I just want to know, would the
>> objects of MyClass collected in principle. Can I achieve the goal
>somehow?
>
>What about creating a lot of them in a loop, without keeping references?
>Then I would expect GC to leave at most one instance.

They can't be created by .new, they created in correspondence to some HTML
DOM by C extension.
Problem already solved by the trick:

--
def test
span = ....
ObjectSpace.define_finalizer(span, proc{p "gc ok"})
span.detach
span = nil
end

test

GC.start

p "end of code"
--

Output corresponds to my expectations (gc ok => end of code)

Thanks for your help!

V.


slunky

11/14/2009 2:35:00 PM

0

Ilya loses.

--
-slunky

The Rock

11/14/2009 4:08:00 PM

0



"Ilya Shambat" <bettermillenium@gmail.com> wrote in message
news:33e66029-39d2-4f2e-ba0b-c9f906c6afa4@u36g2000prn.googlegroups.com...
> On Nov 4, 8:50 pm, tooly <rd...@bellsouth.net> wrote:
>> It's pretty simple really.
>>
>> Republicans are the party of TRADITIONAL America that represents it's
>> constitution and founders.
> ....
>
> Another error is that of equating Republicans with America and giving
> Republicans the credit for all of America's accomplishments. It is
> largely for the sake of transparency that this claim put to rest.
> Here
> are the facts:
> - The Democratic administrations of Woodrow Wilson and Franklin
> Roosevelt won, respectively, the First and the Second World War;
> - The Democrats form the vast bulk of America's scientists and
> innovators, with over 90% of its Nobel Prize winners being Democrats
> and only 3% of academics being Republicans;
> - Of America's 10 foremost educational institutions, 9 are in solidly
> Democratic states and are centers of Democratic Party's activities;
> - The computer industry, which has been the main engine of prosperity
> of 1980s and 1990s, is largely Democratic. The personal computer was
> invented by a hippie. The computer industry is located in solidly
> Democratic San Francisco Bay Area and Washington State, as well as in
> the Democratic-voting Northern Virginia. The Democrats make up the
> bulk of both its management and its rank-and-file.
> - The Clinton Administration gave America its greatest peacetime
> economic expansion (the greatest wartime one having been under
> Franklin Delano Roosevelt). Its policies created 23 million jobs, the
> first balanced budget in three decades, international peace and
> prosperity, and a revamping of the government to become efficient and
> user friendly (Reagan only whined about the government while being
> its
> head). It also brought about a significant reduction in violent crime
> - which grew under Reagan and peaked under Bush Sr. Meanwhile the
> Bush
> Jr. administration added $5 trillion to the debt, with no jobs
> created, amid collapsing family incomes, and at the end the worst
> economic crisis America has had since the Great Depression.
> - Democrats form the bulk of America's teachers and journalists,
> whose
> contributions are not easily measured monetarily but are of greater
> significance than those of many far higher-paid professionals.
> - Democrats have fought for civil rights, human rights, and women's
> rights, while Republicans have done all they could to sabotage all of
> the above.
> - Democrats have fought to bring to public attention the facts about
> global warming. Republicans have done nothing but aggressively deny
> reality - on this, as much as on federal debt.
> The people who blame all things on "liberal government" or "big
> government" or compare American government to the Soviet Union know
> nothing of what they're talking about. Without the government
> Interstate, the anti-government truckers would not have the roads on
> which to deliver the farming goods, created by government-subsidized
> anti-government farmers, to the big liberal cities that are the
> market
> for their products. Without the government academia, the academic
> research that has been necessary for the hardware and technology that
> are at the core of business production would not have taken place.
> Without the government Internet, the computerization and Internet
> commerce that was the source of 1990s growth would not have been
> possible. Whining about taxes was a big trend under the Clinton
> administration. Being in the high-income bracket, I had a lot of
> taxes
> to whine about. I did not whine about my taxes, because I knew where
> the money was going and saw it as my civic duty to pay it. Under Bush
> I would have had less taxes to whine about; but there was a problem.
> Like many others, under Bush, I no longer was in the high-income
> bracket - or, for some of that time, in any bracket at all.
> But beyond personal experience, let us look at the big issues. Who
> has
> given America a $10 trillion debt? Who has aggressively for three
> decades denied global warming? Who has done this to the country they
> claim to love, and to the children and grandchildren for whose sake
> they claim to practice "family values?" It wasn't the Democrats. It
> is
> the Republicans who have done this to America and to their children's
> and grandchildren's future.
> So to put it shortly: Democrats are not Communists; Republicans are
> not America, nor do they deserve credit for America. Case closed.

Thank you, Ilya, well said, and the truth.
Just right now we have Republicans blocking healthcare for America, we
have Republican blocking benefits for veterans, and they'll continue to
block
everything for the average American. That's their philosophy, only the very
rich and big corporations should have anything. And the morons vote for
them!





--
www.lightningnews.com Lightning fast anonymous usenet downloads for 5$ only !

slunky

11/14/2009 4:28:00 PM

0

_/ The Rock <nevermind@nospam.com> wrote \_
> Thank you, Ilya, well said, and the truth.
> Just right now we have Republicans blocking healthcare for America, we
> have Republican blocking benefits for veterans, and they'll continue to
> block
> everything for the average American. That's their philosophy, only the very
> rich and big corporations should have anything. And the morons vote for
> them!

Ilya loses and the librarians laugh heartily.

--
-slunky

slunky

11/19/2009 2:01:00 PM

0

_/ Ilya Shambat <bettermillenium@gmail.com> wrote \_
> On Nov 5, 3:01?am, Robert <RobertAr...@msn.com> wrote:
>> Excellent response by rd.
>> I would add only this:
>>
>> Democratshave changed from the party of John F Kennedy.
>> ButRepublicanshave also changed from the days of Ronald Reagan.
>> The new radicalization is between socialist/statists who worship big
>> government,
>> and libertarian/conservatives who value individual freedom.
>
> More right-wing propaganda. There is individual freedom in places like
> California and New York, which are liberal, and none in the Republican
> areas of the country. Individual freedom has been consistently fought
> for by the Left and opposed by American Right. There was far more
> freedom of every meaningful kind under Clinton than there was under
> Bush, as well as far more prosperity. The most totalitarian group in
> America is the Christian Right. It had the running of the country from
> 2001 to 2008, and the result has been total disaster for America.
>
>> A Canadian friend recently lamented to me that his countrymen made the
>> mistake of trading in their freedom for free things.
>> Progressives in this country (the US) think that's a good trade.
>
> Progressives in US have consistently fought for personal freedom.
> Conservatives have consistently opposed it.
>
> The claim that conservatism has anything to do with freedom is
> nonsense. If you're thoroughly brainwashed into wanting what
> conservatives want you to want, then it's freedom. But it's no freedom
> one should want.

Hi Ilya! Read any good books at the local library lately?

--
-slunky