Skip to content

Instantly share code, notes, and snippets.

@pmahoney
pmahoney / gist:1970815
Created March 4, 2012 05:28
Jenkins and Java fork()+exec() out of memory

Orien is correct, it is the fork() system call triggered by ProcessBuilder or Runtime.exec or other means of the JVM executing an external process (e.g. another JVM running ant, a git command, etc.).

There have been some posts on the Jenkins mailing lists about this: Cannot run program "git" ... error=12, Cannot allocate memory

There is a nice description of the issue on the SCons dev list: fork()+exec() vs posix_spawn()

There is a long standing JVM bug report with solutions: Use posix_spawn, not fork, on S10 to avoid swap exhaustion. But I'm not sure if this actually made it into JDK7 as the comments suggest was the plan.

In summary, on Unix-like systems, when one process (e.g. the JVM) needs to launch another process (e.g. git) a system call is made to

@pmahoney
pmahoney / activerecord test errors
Created May 25, 2012 14:50
activerecord test errors
... similar errors elided ...
16) Error:
test_writing_with_not_nullable_column_encoded_with_JSON(StoreTest):
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a ActiveSupport::HashWithIndifferentAccess, but was a Hash
/home/pat/dev/ruby/rails/activerecord-mine/lib/active_record/coders/yaml_column.rb:34:in `load'
/home/pat/dev/ruby/rails/activerecord-mine/lib/active_record/attribute_methods/serialization.rb:38:in `unserialize'
/home/pat/dev/ruby/rails/activerecord-mine/lib/active_record/attribute_methods/serialization.rb:29:in `unserialized_value'
/home/pat/dev/ruby/rails/activerecord-mine/lib/active_record/attribute_methods/serialization.rb:19:in `type_cast'
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/ruby</directory>
</resource>
</resources>
...
@pmahoney
pmahoney / gist:3503878
Created August 28, 2012 20:32
Ruby Servlet impl skeleton
# Implements some part of the Servlet API in Ruby. Does not extend
# javax.servlet.http.HttpServlet, but implements javax.servlet.Servlet
class MyServlet
include Java::JavaxServlet::Servlet
# @param [javax.servlet.ServletConfig] servletConfig
def init(servletConfig)
end
def destroy
# Runs maven if we are a JRuby process. Currently not used.
#
# See http://watchitlater.com/blog/2011/08/jruby-rake-and-maven/
def mvn(*args)
mvn = catch(:mvn) do
ENV['PATH'].split(::File::PATH_SEPARATOR).each do |path|
Dir.glob(::File.join(path, 'mvn')).each do |file|
if ::File.executable?(file)
throw :mvn, if ::File.symlink?(file)
::File.readlink(file)
@pmahoney
pmahoney / bundle_exec.sh
Created September 5, 2012 21:20
Like bundle exec, but exec's in shell to avoid child process in JRuby 1.6
#!/bin/sh
#
# bundle_exec.sh - emulate 'bundle exec' but with a shell script
# rather than a ruby script. Modifies the environment just like
# bundle exec would.
#
# Running "bundle exec ruby" starts a ruby in which then execs the
# command, 'ruby'. In JRuby 1.6, the command is started in a child
# process. When using a Foreman-like tool that has the ability to
# kill and/or restart such a command, only the parent will be killed
@pmahoney
pmahoney / Gemfile.lock
Created September 13, 2012 13:57
Eventual Gemfile.lock
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
activerecord (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
arel (~> 3.0.2)
@pmahoney
pmahoney / results
Created September 13, 2012 15:50
Crummy kind_of? or respond_to? benchmark under JRuby
require 'java'
require 'benchmark'
s = Java::JavaLang::String.new('hello')
n = 10000000
respond_task = proc { n.times { s.respond_to?(:to_s) || abort('error') } }
no_respond_task = proc { n.times { s.respond_to?(:no_method) && abort('error') } }
kind_of_task = proc { n.times { s.kind_of?(Java::JavaLang::String) || abort('error') } }
no_kind_of_task = proc { n.times { s.kind_of?(Java::JavaLang::Integer) && abort('error') } }
@pmahoney
pmahoney / executor.rb
Created October 3, 2012 20:50
executors in jruby example
require 'java'
class Task
include Java::JavaUtilConcurrent::Callable
def initialize(num)
@num = num
end
def call
@pmahoney
pmahoney / Time.java
Created October 18, 2012 17:29
RubyTime opPlusNanos maybe
public class Time {
private final long timeInMillis;
private final long nsec;
public Time(long timeInMillis, long nsec) {
this.timeInMillis = timeInMillis;
this.nsec = nsec;
}