malloc_trim 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 48a824aec401951c9489513669dcad6f1b1108131fdb86f215d6359564e9e879
4
+ data.tar.gz: 5eba7e5173f70b395fe5d6a04bc6f6747891d523b52ba86d8f3c9f64655d4b24
5
+ SHA512:
6
+ metadata.gz: a57d3731667f148921ea4d0e3027b0bc873507c2bb83bde5db70f0ed160575d453568d68efdf7d859e8ce0f2a3b7ed5439a94c9d6ece5bbf5dc46149853f463f
7
+ data.tar.gz: f6f7ced29bf4b66540e0d09340a4cc275a9709c941df5c2f0c07c076c671be53731eae4d04a55b56b59c598c2fd9b6f46b90cdc180745cee2c3a3387b28f7cdf
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at philipp@tessenow.org. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in malloc_trim.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Philipp Tessenow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # MallocTrim
2
+
3
+ There is currently a proposal for the ruby language to call malloc_trim(0) on GC runs to more efficiently give memory back to the operating system. This is a gem giving access to malloc_trim to ruby land to ease testing.
4
+
5
+ ```ruby
6
+ require 'malloc_trim'
7
+ MallocTrim.trim # most effective when doing a GC.start before
8
+ ```
9
+
10
+ For an explanation on why this works, read:
11
+
12
+ * https://www.joyfulbikeshedding.com/blog/2019-03-14-what-causes-ruby-memory-bloat.html
13
+
14
+ And find the official proposal for introducing this into ruby here:
15
+
16
+ * https://bugs.ruby-lang.org/issues/15667
17
+
18
+ Since `malloc_trim` is not available on MacOS systems, this won't have any effect there. Call `MallocTrim.trimming_possible?` to check whether `malloc_trim` is available.
19
+
20
+ For testing purposes, this gem also provides `MallocTrim.enable_trimming` (and it's counterpart `disable_trimming`) which calls `malloc_trim` after every GC run.
21
+
22
+ I, personally, found it most effective when running
23
+
24
+ ```
25
+ GC.start
26
+ MallocTrim.trim
27
+ ```
28
+
29
+ after a heavy job. This frees memory where repeated calls to only `GC.start` does not free any memory.
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ ```ruby
36
+ gem 'malloc_trim'
37
+ ```
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install malloc_trim
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Note that MacOS systems don't ship with `malloc_trim` so this gem will have nothing to do on these systems.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tessi/malloc_trim. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
60
+
61
+ ## Code of Conduct
62
+
63
+ Everyone interacting in the MallocTrim project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tessi/malloc_trim/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rake/extensiontask"
7
+
8
+ task :build => :compile
9
+
10
+ Rake::ExtensionTask.new("malloc_trim") do |ext|
11
+ ext.lib_dir = "lib/malloc_trim"
12
+ end
13
+
14
+ task :default => [:clobber, :compile, :spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "malloc_trim"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,266 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@ :)
9
+ ECHO = $(ECHO1:0=@ echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /Users/tessi/.asdf/installs/ruby/2.6.0/include/ruby-2.6.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /Users/tessi/.asdf/installs/ruby/2.6.0/include/ruby-2.6.0/x86_64-darwin18
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
20
+ prefix = $(DESTDIR)/Users/tessi/.asdf/installs/ruby/2.6.0
21
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
22
+ rubyarchprefix = $(rubylibprefix)/$(arch)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
26
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
27
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(sitelibdir)/$(sitearch)
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(rubylibprefix)/site_ruby
37
+ rubyarchdir = $(rubylibdir)/$(arch)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(datarootdir)/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(datarootdir)/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(SDKROOT)/usr/include
54
+ includedir = $(prefix)/include
55
+ localstatedir = $(prefix)/var
56
+ sharedstatedir = $(prefix)/com
57
+ sysconfdir = $(prefix)/etc
58
+ datadir = $(datarootdir)
59
+ datarootdir = $(prefix)/share
60
+ libexecdir = $(exec_prefix)/libexec
61
+ sbindir = $(exec_prefix)/sbin
62
+ bindir = $(exec_prefix)/bin
63
+ archdir = $(rubyarchdir)
64
+
65
+
66
+ CC_WRAPPER =
67
+ CC = clang
68
+ CXX = clang++
69
+ LIBRUBY = $(LIBRUBY_A)
70
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
71
+ LIBRUBYARG_SHARED =
72
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static -framework Security -framework Foundation $(MAINLIBS)
73
+ empty =
74
+ OUTFLAG = -o $(empty)
75
+ COUTFLAG = -o $(empty)
76
+ CSRCFLAG = $(empty)
77
+
78
+ RUBY_EXTCONF_H =
79
+ cflags = $(optflags) $(debugflags) $(warnflags)
80
+ cxxflags = $(optflags) $(debugflags) $(warnflags)
81
+ optflags = -O3
82
+ debugflags = -ggdb3
83
+ warnflags = -Wall -Wextra -Wdeclaration-after-statement -Wdeprecated-declarations -Wdivision-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wmissing-noreturn -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wextra-tokens
84
+ cppflags =
85
+ CCDLFLAGS = -fno-common
86
+ CFLAGS = $(CCDLFLAGS) -O3 -Wno-error=shorten-64-to-32 -pipe $(ARCH_FLAG)
87
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
88
+ DEFS =
89
+ CPPFLAGS = -I/Users/tessi/.asdf/installs/ruby/2.6.0/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT $(DEFS) $(cppflags)
90
+ CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG)
91
+ ldflags = -L. -L/Users/tessi/.asdf/installs/ruby/2.6.0/lib -fstack-protector-strong -L/usr/local/lib
92
+ dldflags = -L/Users/tessi/.asdf/installs/ruby/2.6.0/lib -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress
93
+ ARCH_FLAG =
94
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
95
+ LDSHARED = $(CC) -dynamic -bundle
96
+ LDSHAREDXX = $(CXX) -dynamic -bundle
97
+ AR = libtool -static
98
+ EXEEXT =
99
+
100
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
101
+ RUBY_SO_NAME = ruby.2.6
102
+ RUBYW_INSTALL_NAME =
103
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
104
+ RUBYW_BASE_NAME = rubyw
105
+ RUBY_BASE_NAME = ruby
106
+
107
+ arch = x86_64-darwin18
108
+ sitearch = $(arch)
109
+ ruby_version = 2.6.0
110
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
111
+ RUBY = $(ruby)
112
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
113
+
114
+ RM = rm -f
115
+ RM_RF = $(RUBY) -run -e rm -- -rf
116
+ RMDIRS = rmdir -p
117
+ MAKEDIRS = /usr/local/bin/gmkdir -p
118
+ INSTALL = /usr/local/bin/ginstall -c
119
+ INSTALL_PROG = $(INSTALL) -m 0755
120
+ INSTALL_DATA = $(INSTALL) -m 644
121
+ COPY = cp
122
+ TOUCH = exit >
123
+
124
+ #### End of system configuration section. ####
125
+
126
+ preload =
127
+ libpath = . $(libdir)
128
+ LIBPATH = -L. -L$(libdir)
129
+ DEFFILE =
130
+
131
+ CLEANFILES = mkmf.log
132
+ DISTCLEANFILES =
133
+ DISTCLEANDIRS =
134
+
135
+ extout =
136
+ extout_prefix =
137
+ target_prefix = /malloc_trim
138
+ LOCAL_LIBS =
139
+ LIBS =
140
+ ORIG_SRCS = malloc_trim.c
141
+ SRCS = $(ORIG_SRCS)
142
+ OBJS = malloc_trim.o
143
+ HDRS = $(srcdir)/malloc_trim.h
144
+ LOCAL_HDRS =
145
+ TARGET = malloc_trim
146
+ TARGET_NAME = malloc_trim
147
+ TARGET_ENTRY = Init_$(TARGET_NAME)
148
+ DLLIB = $(TARGET).bundle
149
+ EXTSTATIC =
150
+ STATIC_LIB =
151
+
152
+ TIMESTAMP_DIR = .
153
+ BINDIR = $(bindir)
154
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
155
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
156
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
157
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
158
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
159
+ TARGET_SO_DIR =
160
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
161
+ CLEANLIBS = $(TARGET_SO)
162
+ CLEANOBJS = *.o *.bak
163
+
164
+ all: $(DLLIB)
165
+ static: $(STATIC_LIB)
166
+ .PHONY: all install static install-so install-rb
167
+ .PHONY: clean clean-so clean-static clean-rb
168
+
169
+ clean-static::
170
+ clean-rb-default::
171
+ clean-rb::
172
+ clean-so::
173
+ clean: clean-so clean-static clean-rb-default clean-rb
174
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
175
+
176
+ distclean-rb-default::
177
+ distclean-rb::
178
+ distclean-so::
179
+ distclean-static::
180
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
181
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
182
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
183
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
184
+
185
+ realclean: distclean
186
+ install: install-so install-rb
187
+
188
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.sitearchdir.-.malloc_trim.time
189
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
190
+ clean-static::
191
+ -$(Q)$(RM) $(STATIC_LIB)
192
+ install-rb: pre-install-rb do-install-rb install-rb-default
193
+ install-rb-default: pre-install-rb-default do-install-rb-default
194
+ pre-install-rb: Makefile
195
+ pre-install-rb-default: Makefile
196
+ do-install-rb:
197
+ do-install-rb-default:
198
+ pre-install-rb-default:
199
+ @$(NULLCMD)
200
+ $(TIMESTAMP_DIR)/.sitearchdir.-.malloc_trim.time:
201
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
202
+ $(Q) $(TOUCH) $@
203
+
204
+ site-install: site-install-so site-install-rb
205
+ site-install-so: install-so
206
+ site-install-rb: install-rb
207
+
208
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
209
+
210
+ .cc.o:
211
+ $(ECHO) compiling $(<)
212
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
213
+
214
+ .cc.S:
215
+ $(ECHO) translating $(<)
216
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
217
+
218
+ .mm.o:
219
+ $(ECHO) compiling $(<)
220
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
221
+
222
+ .mm.S:
223
+ $(ECHO) translating $(<)
224
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
225
+
226
+ .cxx.o:
227
+ $(ECHO) compiling $(<)
228
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
229
+
230
+ .cxx.S:
231
+ $(ECHO) translating $(<)
232
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
233
+
234
+ .cpp.o:
235
+ $(ECHO) compiling $(<)
236
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
237
+
238
+ .cpp.S:
239
+ $(ECHO) translating $(<)
240
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
241
+
242
+ .c.o:
243
+ $(ECHO) compiling $(<)
244
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
245
+
246
+ .c.S:
247
+ $(ECHO) translating $(<)
248
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
249
+
250
+ .m.o:
251
+ $(ECHO) compiling $(<)
252
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
253
+
254
+ .m.S:
255
+ $(ECHO) translating $(<)
256
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
257
+
258
+ $(TARGET_SO): $(OBJS) Makefile
259
+ $(ECHO) linking shared-object malloc_trim/$(DLLIB)
260
+ -$(Q)$(RM) $(@)
261
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
262
+ $(Q) $(POSTLINK)
263
+
264
+
265
+
266
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -0,0 +1,7 @@
1
+ require "mkmf"
2
+
3
+ find_header("malloc.h")
4
+ find_header("ruby/debug.h")
5
+ have_func("malloc_trim")
6
+ abort 'error: needs ruby 2.1+' unless have_func('rb_tracepoint_new') && have_const('RUBY_INTERNAL_EVENT_GC_END_SWEEP')
7
+ create_makefile("malloc_trim/malloc_trim")
@@ -0,0 +1,70 @@
1
+ #include "malloc_trim.h"
2
+
3
+ VALUE rb_mMallocTrim;
4
+
5
+ static VALUE objtracer;
6
+
7
+ static VALUE do_malloc_trim(VALUE self) {
8
+ /* [Experimental] Explicitly free all eligible pages to the kernel. See:
9
+ *
10
+ * - https://www.joyfulbikeshedding.com/blog/2019-03-14-what-causes-ruby-memory-bloat.html
11
+ * - https://bugs.ruby-lang.org/issues/15667
12
+ * - http://man7.org/linux/man-pages/man3/malloc_trim.3.html
13
+ */
14
+ #ifdef HAVE_MALLOC_TRIM
15
+ if (malloc_trim(0) == 1) {
16
+ return Qtrue; // it could return some memory
17
+ } else {
18
+ return Qfalse; // no memory was returned
19
+ };
20
+ #endif
21
+ return Qnil;
22
+ }
23
+
24
+ static VALUE has_malloc_trim(VALUE self) {
25
+ #ifdef HAVE_MALLOC_TRIM
26
+ return Qtrue;
27
+ #else
28
+ return Qfalse;
29
+ #endif
30
+ }
31
+
32
+ static void malloc_trim_gc_end_handler(VALUE tpval, void *data) {
33
+ #ifdef HAVE_MALLOC_TRIM
34
+ malloc_trim(0);
35
+ #endif
36
+ }
37
+
38
+ static VALUE enable_trimming(VALUE self) {
39
+ #ifndef HAVE_MALLOC_TRIM
40
+ return Qnil;
41
+ #endif
42
+
43
+ if (!RB_TEST(objtracer)) {
44
+ objtracer = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_END_MARK, malloc_trim_gc_end_handler, 0);
45
+ }
46
+ rb_tracepoint_enable(objtracer);
47
+ return Qtrue;
48
+ }
49
+
50
+ static VALUE disable_trimming(VALUE self) {
51
+ #ifndef HAVE_MALLOC_TRIM
52
+ return Qnil;
53
+ #endif
54
+
55
+ if (RB_TEST(objtracer)) {
56
+ rb_tracepoint_disable(objtracer);
57
+ }
58
+ objtracer = Qnil;
59
+ return Qtrue;
60
+ }
61
+
62
+ void
63
+ Init_malloc_trim(void)
64
+ {
65
+ rb_mMallocTrim = rb_define_module("MallocTrim");
66
+ rb_define_singleton_method(rb_mMallocTrim, "trim", do_malloc_trim, 0);
67
+ rb_define_singleton_method(rb_mMallocTrim, "trimming_possible?", has_malloc_trim, 0);
68
+ rb_define_singleton_method(rb_mMallocTrim, "enable_trimming", enable_trimming, 0);
69
+ rb_define_singleton_method(rb_mMallocTrim, "disable_trimming", disable_trimming, 0);
70
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef MALLOC_TRIM_H
2
+ #define MALLOC_TRIM_H 1
3
+
4
+ #include "ruby.h"
5
+ #include "ruby/debug.h"
6
+
7
+ #ifdef HAVE_MALLOC_TRIM
8
+ #include "malloc.h"
9
+ #endif
10
+
11
+ #endif /* MALLOC_TRIM_H */
@@ -0,0 +1,6 @@
1
+ require "malloc_trim/version"
2
+ require "malloc_trim/malloc_trim"
3
+
4
+ module MallocTrim
5
+ class Error < StandardError; end
6
+ end
@@ -0,0 +1,3 @@
1
+ module MallocTrim
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "malloc_trim/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "malloc_trim"
8
+ spec.version = MallocTrim::VERSION
9
+ spec.authors = ["Philipp Tessenow"]
10
+ spec.email = ["philipp@tessenow.org"]
11
+
12
+ spec.summary = %q{Gives some memory held by a ruby process back to the OS}
13
+ spec.description = %q{There is currently a proposal for the ruby language to call malloc_trim(0) on GC runs to more efficiently give memory back to the operating system. This is a gem giving access to malloc_trim to ruby land to ease testing.}
14
+ spec.homepage = "https://github.com/tessi/malloc_trim"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+ spec.extensions = ["ext/malloc_trim/extconf.rb"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.17"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rake-compiler"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: malloc_trim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philipp Tessenow
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: There is currently a proposal for the ruby language to call malloc_trim(0)
70
+ on GC runs to more efficiently give memory back to the operating system. This is
71
+ a gem giving access to malloc_trim to ruby land to ease testing.
72
+ email:
73
+ - philipp@tessenow.org
74
+ executables: []
75
+ extensions:
76
+ - ext/malloc_trim/extconf.rb
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".travis.yml"
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - ext/malloc_trim/Makefile
90
+ - ext/malloc_trim/extconf.rb
91
+ - ext/malloc_trim/malloc_trim.c
92
+ - ext/malloc_trim/malloc_trim.h
93
+ - lib/malloc_trim.rb
94
+ - lib/malloc_trim/version.rb
95
+ - malloc_trim.gemspec
96
+ homepage: https://github.com/tessi/malloc_trim
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Gives some memory held by a ruby process back to the OS
119
+ test_files: []