Skip to content

Commit 1df0ada

Browse files
author
Glyn Normington
committed
Set heap and permgen sizes to maximum values
Set -Xms to the same value as -Xms and -XX:PermSize to the same value as -XX:MaxPermSize. This ensures that the committed memory for the heap and the permanent generation does not increase during the lifetime of the application. This should reduce the possibility of warden killing the JVM process once the application is running. [#59731874]
1 parent 8f055b9 commit 1df0ada

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

lib/java_buildpack/jre/memory/openjdk_memory_heuristic_factory.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def self.permgen_or_metaspace(version)
4343
VALID_TYPES = %w(heap stack native).freeze
4444

4545
JAVA_OPTS = {
46-
'heap' => '-Xmx',
47-
'metaspace' => '-XX:MaxMetaspaceSize=',
48-
'permgen' => '-XX:MaxPermSize=',
49-
'stack' => '-Xss',
46+
'heap' => ->(v) { "-Xmx#{v} -Xms#{v}" },
47+
'metaspace' => ->(v) { "-XX:MaxMetaspaceSize=#{v}" },
48+
'permgen' => ->(v) { "-XX:MaxPermSize=#{v} -XX:PermSize=#{v}" },
49+
'stack' => ->(v) { "-Xss#{v}" }
5050
}.freeze
5151

5252
end

lib/java_buildpack/jre/memory/weight_balancing_memory_heuristic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def check_close_to_default(type, bucket, total_weighting)
220220
end
221221

222222
def set_switches(buckets)
223-
buckets.map { |type, bucket| "#{@java_opts[type]}#{bucket.size}" if bucket.size && bucket.size > 0 && @java_opts.key?(type) }.compact
223+
buckets.map { |type, bucket| @java_opts[type][bucket.size] if bucket.size && bucket.size > 0 && @java_opts.key?(type) }.compact
224224
end
225225

226226
end

spec/java_buildpack/jre/memory/openjdk_memory_heuristic_factory_spec.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,41 @@ module JavaBuildpack::Jre
2626
HEURISTICS = { 'c' => 'd' }
2727
PRE_8 = JavaBuildpack::Util::TokenizedVersion.new('1.7.0')
2828
POST_8 = JavaBuildpack::Util::TokenizedVersion.new('1.8.0')
29-
EXPECTED_JAVA_MEMORY_OPTIONS = { 'heap' => '-Xmx', 'metaspace' => '-XX:MaxMetaspaceSize=', 'permgen' => '-XX:MaxPermSize=', 'stack' => '-Xss' }
29+
EXPECTED_JAVA_MEMORY_OPTIONS = {
30+
'heap' => ->(v) { "-Xmx#{v} -Xms#{v}" },
31+
'metaspace' => ->(v) { "-XX:MaxMetaspaceSize=#{v}" },
32+
'permgen' => ->(v) { "-XX:MaxPermSize=#{v} -XX:PermSize=#{v}" },
33+
'stack' => ->(v) { "-Xss#{v}" }
34+
}
35+
36+
class HashOfLambdasMatching
37+
def initialize(hash)
38+
@hash = hash
39+
end
40+
41+
def ==(other)
42+
@hash['heap']['1m'] == other['heap']['1m'] &&
43+
@hash['metaspace']['2m'] == other['metaspace']['2m'] &&
44+
@hash['permgen']['3m'] == other['permgen']['3m'] &&
45+
@hash['stack']['4m'] == other['stack']['4m']
46+
end
47+
48+
def inspect
49+
"a hash containing lambdas #{@hash.inspect}"
50+
end
51+
end
52+
53+
def hash_of_lambdas_matching(hash)
54+
HashOfLambdasMatching.new(hash)
55+
end
3056

3157
it 'should pass the appropriate constructor parameters for versions prior to 1.8' do
32-
WeightBalancingMemoryHeuristic.stub(:new).with(SIZES, HEURISTICS, %w(heap stack native permgen), EXPECTED_JAVA_MEMORY_OPTIONS)
58+
WeightBalancingMemoryHeuristic.stub(:new).with(SIZES, HEURISTICS, %w(heap stack native permgen), hash_of_lambdas_matching(EXPECTED_JAVA_MEMORY_OPTIONS))
3359
OpenJDKMemoryHeuristicFactory.create_memory_heuristic(SIZES, HEURISTICS, PRE_8)
3460
end
3561

3662
it 'should pass the appropriate constructor parameters for versions 1.8 and higher' do
37-
WeightBalancingMemoryHeuristic.stub(:new).with(SIZES, HEURISTICS, %w(heap stack native metaspace), EXPECTED_JAVA_MEMORY_OPTIONS)
63+
WeightBalancingMemoryHeuristic.stub(:new).with(SIZES, HEURISTICS, %w(heap stack native metaspace), hash_of_lambdas_matching(EXPECTED_JAVA_MEMORY_OPTIONS))
3864
OpenJDKMemoryHeuristicFactory.create_memory_heuristic(SIZES, HEURISTICS, POST_8)
3965
end
4066
end

spec/java_buildpack/jre/memory/weight_balancing_memory_heuristic_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ module JavaBuildpack::Jre
3535
}
3636

3737
PRE8_JAVA_OPTS = {
38-
'heap' => '-Xmx',
39-
'permgen' => '-XX:MaxPermSize=',
40-
'stack' => '-Xss',
38+
'heap' => ->(v) { "-Xmx#{v}" },
39+
'permgen' => ->(v) { "-XX:MaxPermSize=#{v}" },
40+
'stack' => ->(v) { "-Xss#{v}" }
4141
}.freeze
4242

4343
PRE8_VALID_TYPES = %w(heap permgen stack native)

0 commit comments

Comments
 (0)