# Release a resource from memory after executing?

**URL:** <https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862>\
**Category:** Chef Infra (archive)\
**Created:** [November 2, 2017, 6:11pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862 "2017-11-02T18:11:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![327TimesBetter](https://avatars.discourse-cdn.com/v4/letter/3/e19adc/32.png) [@327TimesBetter](https://discourse.chef.io/u/327TimesBetter)\
**Post date:** [November 2, 2017, 6:11pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862/1 "2017-11-02T18:11:11Z")

</div>

I have a cookbook where we generate a particular execute resource in a loop. Recently, the number of items in the loop has been growing so large that the chef-client runs out of memory trying to hold all of the execute resources in memory.

I have tried adding execute\_resource.run\_action(:run) in the hopes that it would simply execute the resource at compile time and then not have to hold them all but to no avail.

Any thoughts on how we could prevent chef-client from running out of memory while attempting to do this??

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [November 2, 2017, 6:31pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862/2 "2017-11-02T18:31:58Z")

</div>

That would mean something like millions or billions of resources unless this is on a RasPi. At that point it’s probably time for a dedicated tool that Chef just runs.

---

<div class="post-metadata">

**Author:** ![327TimesBetter](https://avatars.discourse-cdn.com/v4/letter/3/e19adc/32.png) [@327TimesBetter](https://discourse.chef.io/u/327TimesBetter)\
**Post date:** [November 2, 2017, 7:08pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862/3 "2017-11-02T19:08:23Z")

</div>

The loop is on a knife search command for all nodes using a particular recipe, and we certainly dont have millions or billions of nodes - probably something more like 700~1000.

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [November 2, 2017, 8:46pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862/4 "2017-11-02T20:46:00Z")

</div>

> [@327TimesBetter](#):
>
> The loop is on a knife search command for all nodes using a particular recipe,

Most likely your issue is loading all the node data from all those into memory. Have a look at search filtering to have the Chef Server pre-filter the results for you: [About Search](https://docs.chef.io/chef_search.html#filter-search-results)

---

<div class="post-metadata">

**Author:** ![327TimesBetter](https://avatars.discourse-cdn.com/v4/letter/3/e19adc/32.png) [@327TimesBetter](https://discourse.chef.io/u/327TimesBetter)\
**Post date:** [November 6, 2017, 3:13pm UTC](https://discourse.chef.io/t/release-a-resource-from-memory-after-executing/11862/5 "2017-11-06T15:13:23Z")

</div>

That was exactly the issue. We only use the attribute [“ipadress”] in our code, so I was able to change

```
search( :node, search_string ) do |n|

```

in to

```
search(:node, search_string, :filter_result => { "ipaddress" => ["ipaddress"] } ) do |n|

```

and now our chef-client runs using that cookbook still take a super long time as it processes a 700-something item loop, but they complete smoothly with pretty much 0 memory footprint.
