# Improvement: better memory usage in dry-monitor

**URL:** <https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468>\
**Category:** Ideas\
**Created:** [February 7, 2018, 12:30am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468 "2018-02-07T00:30:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alejandrobabio](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alejandrobabio/32/120_2.png) [@alejandrobabio](https://discourse.dry-rb.org/u/alejandrobabio)\
**Post date:** [February 7, 2018, 12:30am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468/1 "2018-02-07T00:30:12Z")

</div>

After testing a dry-web-roda app with the gem `derailed_benchmarks` I found that my app was using more memory than a bigger Rails app.

Actually, this is the first part of the output:

```auto
$ bundle exec derailed bundle:mem
TOP: 42.4297 MiB
  dry-web: 13.6484 MiB
    dry/web/container: 13.6484 MiB
      dry/monitor: 13.4023 MiB
        dry/monitor/sql/logger: 12.9414 MiB
          rouge: 12.9414 MiB

```

That means, that the app is using 42 MiB, and the `dry-web` component is the biggest one, that is using 13.6 MiB, because of `rouge` (12.9 MiB).

So, requiring only the needed components of `rouge`, by changing in `lib/dry/monitor/sql/logger.rb` the line 2:

```ruby
require 'rouge'

```

with:

```ruby
require 'rouge/util'
require 'rouge/token'
require 'rouge/theme'
require 'rouge/themes/gruvbox'
require 'rouge/formatter'
require 'rouge/formatters/terminal256'
require 'rouge/lexer'
require 'rouge/regex_lexer'
require 'rouge/lexers/sql'

```

We can get an improvement. The mem test after that returns:

```auto
$ bundle exec derailed bundle:mem
TOP: 26.1211 MiB
  rom: 8.0352 MiB
    ...
  dry-web: 1.1211 MiB
    dry/web/container: 1.1211 MiB
      dry/monitor: 0.75 MiB
        dry/monitor/sql/logger: 0.5 MiB

```

So, the memory used by dry-monitor was reduced from 13.4 MiB to 0.75 Mib. And the impact on the memory usage of a small dry-web-roda app was bigger than that: from 42.4297 MiB to 26.1211 MiB.

I did not analyze how `derailed_benchmarks` make its maths. But it’s clear that `rouge` has a lot of cool components that are not used by `dry-monitor`. Then, Why do not get rid of it?

Please, let me know what you think about this. Thank you.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [February 7, 2018, 9:01am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468/2 "2018-02-07T09:01:02Z")

</div>

Oh wow, I did not expect that. I’m all for limiting requires to what you actually need. In fact, I’m a strong believer that all gems should be designed in a way that they always provide cherry-pickable components.

Would you mind sending a PR to dry-monitor? 🙂

---

<div class="post-metadata">

**Author:** ![davydovanton](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/davydovanton/32/211_2.png) [@davydovanton](https://discourse.dry-rb.org/u/davydovanton)\
**Post date:** [February 9, 2018, 8:57am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468/3 "2018-02-09T08:57:47Z")

</div>

@solnic also, I checked my job project for object allocation and found interesting things:

```auto
$ bundle exec derailed bundle:objects

Measuring objects created by gems in groups [:default, "production"]
Total allocated: 91115563 bytes (800628 objects)
Total retained: 15093944 bytes (134684 objects)

allocated memory by gem
-----------------------------------
  10664845 rouge-2.2.1
   8722637 ruby-2.4.2/lib
   8059914 newrelic_rpm-4.2.0.334
   6488996 mime-types-3.1
   4489137 dry-initializer-2.3.0

```

As you can see, rouge allocate more objects raise than other dependencies. And also, this dependency needs only for color output. Sometimes it can break production (when logger services doesn’t support the color format, for example).

That’s why I have a question: WDYT if we make `rouge` dependency as optional? I mean if a user wants to use `rouge` they can install the gem and that’s all. Also, a user gets control on color/not color logs for any environments.

Something like this (check [logger file](https://github.com/dry-rb/dry-monitor/blob/3e1c92a5b066c20bfb86c59da9beb310a5f9b0d7/lib/dry/monitor/sql/logger.rb#L52)):

```ruby
begin
  require 'rouge'

  module Dry
    module Monitor

      module SQL
        class Logger
          # use rouge lexer and other stuff
        end
      end
    end
  end
rescue LoadError
  module Dry
    module Monitor

      module SQL
        class Logger
          # logger without color formatter
        end
      end
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [February 10, 2018, 10:10am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468/4 "2018-02-10T10:10:16Z")

</div>

> [@davydovanton](#):
>
> That’s why I have a question: WDYT if we make rouge dependency as optional?

Yeah, it should be optional. I just wanted to have colors and put it there by default. Logger is configurable so it is very easy to add config setting for disabling colors.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [February 10, 2018, 10:24am UTC](https://discourse.dry-rb.org/t/improvement-better-memory-usage-in-dry-monitor/468/5 "2018-02-10T10:24:11Z")

</div>

Quick update, I reported an issue about it [https://github.com/dry-rb/dry-monitor/issues/9](https://github.com/dry-rb/dry-monitor/issues/9) I also discovered that I actually made colorful output configurable already 😅
