# Does this code spawn lots of objects?

**URL:** <https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587>\
**Category:** Ruby\
**Created:** [May 31, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587 "2007-05-31T13:50:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [May 31, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587/1 "2007-05-31T13:50:00Z")

</div>

I have the following code:-

dbh = DBI.connect(’_’, '_’, ‘_’)  
smtp = Net::SMTP.start(’_’)  
sql = open( “gw.sql”, “r” ).read  
sth = dbh.execute( sql )  
sth.fetch\_hash do |row|  
mail = MailFactory.new()  
mail.to = “removed\_email\_address@domain.invalid”  
mail.from = “removed\_email\_address@domain.invalid”  
mail.subject = “file from ingenta”  
mail.text = “Here is the file”  
mail.attach( “./” + row[“IDENTITYID”] + “\_” + month + year + “.csv” );  
smtp.send\_message( mail.to\_s(), mail.from, mail.to )  
end  
sth.finish  
smtp.finish  
dbh.disconnect

This code seems to cause a new object to be created for every  
iteration of the iterator. However is it true that ‘mail =  
MailFactory.new()’ causes the previous Mailfactory object not to have  
a reference to it and therefore it can be garbage collected?

Is there a better way of coding the above?

Ben

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [May 31, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587/2 "2007-05-31T14:01:01Z")

</div>

On 31.05.2007 13:49, Ben E. wrote:

> mail.subject = “file from ingenta”  
> MailFactory.new()’ causes the previous Mailfactory object not to have  
> a reference to it and therefore it can be garbage collected?
> 
> Is there a better way of coding the above?

Probably not much room for improvement. One thing you can do for sure  
is to use the block form of those various connections / IO’s you are  
using. And you can read in a complete file by doing  
File.read(“ge.sql”).

Probably you can also reuse the MailFactory (from, to, subject and text  
don’t change anyway). But I have no idea whether that will create any  
improvement. And I also do not know that class so take this advice with  
a grain of salt.

Another idea is to start a second thread for mail sending. Connect both  
threads with a queue with limited size. That way you may use IO  
resources more efficiently.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [May 31, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587/3 "2007-05-31T15:41:20Z")

</div>

On 31/05/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > mail.from = “removed\_email\_address@domain.invalid”  
> > iteration of the iterator. However is it true that ‘mail =  
> > MailFactory.new()’ causes the previous Mailfactory object not to have  
> > a reference to it and therefore it can be garbage collected?
> > 
> > Is there a better way of coding the above?
> 
> Probably not much room for improvement. One thing you can do for sure  
> is to use the block form of those various connections / IO’s you are  
> using. And you can read in a complete file by doing File.read(“ge.sql”).

What difference will this make?

Thanks for advice,  
Ben

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [May 31, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587/4 "2007-05-31T15:46:20Z")

</div>

On 31.05.2007 15:42, Ben E. wrote:

> > > mail.to = “removed\_email\_address@domain.invalid”  
> > > This code seems to cause a new object to be created for every  
> > > Sorry, forgot to ask. What do you mean by block form?  
> > > DBI.connect(’_’, '_’, ‘\*’) do |dbh|  
> > > …  
> > > end

You gain safety because it’s guaranteed that the connection is closed  
regardless how the block is left (normal, exception).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [May 31, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/does-this-code-spawn-lots-of-objects/105587/5 "2007-05-31T15:43:20Z")

</div>

On 31/05/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > mail.from = “removed\_email\_address@domain.invalid”  
> > iteration of the iterator. However is it true that ‘mail =  
> > MailFactory.new()’ causes the previous Mailfactory object not to have  
> > a reference to it and therefore it can be garbage collected?
> > 
> > Is there a better way of coding the above?
> 
> Probably not much room for improvement. One thing you can do for sure  
> is to use the block form of those various connections / IO’s you are  
> using. And you can read in a complete file by doing File.read(“ge.sql”).

Sorry, forgot to ask. What do you mean by block form?
