Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Restricting the memory usage of a Java program Date: Thu, 11 Aug 2016 23:42:04 +0200 Organization: Decebal Computing Lines: 29 Message-ID: <8760r7m18z.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="12baf5125b19322b899c79c2e3878015"; logging-data="31566"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HdKm257rtdP/ITG1/ODfoWxEaYBdI8Vs=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:j8d7vbbLDj4TiEmmn/oe6+8/Opk= sha1:3/xnMLiIoifaeBiVHJ8gLi/bbZ8= Xref: csiph.com comp.lang.java.programmer:35262 I made a little program for cropping images. When just starting it as a Java program the memory usage was just way to much. So I wrote the following script: #!/usr/bin/env bash declare -r CLASS=CropImage declare -r MIN_MEM=128M declare -r MAX_MEM=256M # Make sure you are in the right directory. # Only start the Java application when moving to the right directory was succesful. cd "${0%/*}" && java "-Xms${MIN_MEM}" "-Xmx${MAX_MEM}" "${CLASS}" & And now the memory consumption is much less and as far as I can see without negative consequences for the program. The values I use (128 MB and 256 MB) are just values I kind of randomly choose. Is there a way to find ‘optimal’ values? Bonus question: is there a way to limit the memory usage when the program is started with: java CropImage instead of the script? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof