How to deal with “java.lang.OutOfMemoryError: Java heap space” error?How to handle OutOfMemoryError in Java?How to allocate space to JAVA using command lineNETBEANS: “java.lang.OutOfMemoryError: Java heap space”Is there any way to handle Java heap space exceptionJava - Heap memoryjava.lang.OutOfMemoryError: how to set correct Java parametersOutOfMemoryError: Java heap space in a loopHow to fix java.lang.OutOfMemoryError: Java heap space error?Exception in thread “main” java.lang.OutOfMemoryError: Java heap space if i'm scann many folderJava heap Space - OutOfMemory ErrorHow do I efficiently iterate over each entry in a Java Map?Dealing with “java.lang.OutOfMemoryError: PermGen space” errorHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?Error java.lang.OutOfMemoryError: GC overhead limit exceededCould not reserve enough space for object heapHow do I convert a String to an int in Java?

What is a good way to allow only one non null field in an object

Is every story set in the future "science fiction"?

Output the date in the Mel calendar

How did Captain Marvel know where to find these characters?

Company stopped paying my salary. What are my options?

What's an appropriate age to involve kids in life changing decisions?

Ugin's Conjurant vs. un-preventable damage

Probability of taking balls without replacement from a bag question

What dice to use in a game that revolves around triangles?

Why did Missandei say this?

What's the difference between "ricochet" and "bounce"?

resoldering copper waste pipe

How can it be that ssh somename works, while nslookup somename does not?

Gift for mentor after his thesis defense?

Why do the Avengers care about returning these items in Endgame?

Did the IBM System/4 Pi computer have radiation-hardened versions for Skylab and Shuttle?

Identity of a supposed anonymous referee revealed through "Description" of the report

How to handle DM constantly stealing everything from sleeping characters?

How do carbureted and fuel injected engines compare in high altitude?

A Latin text with dependency tree

Has there been evidence of any other gods?

Two (probably) equal real numbers which are not proved to be equal?

Why use steam instead of just hot air?

TeX Gyre Pagella Math Integral sign much too small



How to deal with “java.lang.OutOfMemoryError: Java heap space” error?


How to handle OutOfMemoryError in Java?How to allocate space to JAVA using command lineNETBEANS: “java.lang.OutOfMemoryError: Java heap space”Is there any way to handle Java heap space exceptionJava - Heap memoryjava.lang.OutOfMemoryError: how to set correct Java parametersOutOfMemoryError: Java heap space in a loopHow to fix java.lang.OutOfMemoryError: Java heap space error?Exception in thread “main” java.lang.OutOfMemoryError: Java heap space if i'm scann many folderJava heap Space - OutOfMemory ErrorHow do I efficiently iterate over each entry in a Java Map?Dealing with “java.lang.OutOfMemoryError: PermGen space” errorHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?Error java.lang.OutOfMemoryError: GC overhead limit exceededCould not reserve enough space for object heapHow do I convert a String to an int in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








361















I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found Ergonomics in the 5.0 Java Virtual Machine and others saying on Windows machine the JVM defaults max heap size as 64MB.



Given this situation, how should I deal with this constraint?



I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.



I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.



If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.










share|improve this question
























  • The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

    – Andy Thomas
    Apr 8 '15 at 15:36












  • If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

    – Steve Steiner
    Apr 24 '18 at 20:13

















361















I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found Ergonomics in the 5.0 Java Virtual Machine and others saying on Windows machine the JVM defaults max heap size as 64MB.



Given this situation, how should I deal with this constraint?



I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.



I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.



If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.










share|improve this question
























  • The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

    – Andy Thomas
    Apr 8 '15 at 15:36












  • If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

    – Steve Steiner
    Apr 24 '18 at 20:13













361












361








361


138






I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found Ergonomics in the 5.0 Java Virtual Machine and others saying on Windows machine the JVM defaults max heap size as 64MB.



Given this situation, how should I deal with this constraint?



I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.



I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.



If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.










share|improve this question
















I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found Ergonomics in the 5.0 Java Virtual Machine and others saying on Windows machine the JVM defaults max heap size as 64MB.



Given this situation, how should I deal with this constraint?



I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.



I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.



If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.







java java-ee jvm out-of-memory heap-memory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 8:43









Hearen

3,03211630




3,03211630










asked Sep 1 '08 at 1:10









Eugene YokotaEugene Yokota

74.3k39183280




74.3k39183280












  • The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

    – Andy Thomas
    Apr 8 '15 at 15:36












  • If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

    – Steve Steiner
    Apr 24 '18 at 20:13

















  • The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

    – Andy Thomas
    Apr 8 '15 at 15:36












  • If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

    – Steve Steiner
    Apr 24 '18 at 20:13
















The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

– Andy Thomas
Apr 8 '15 at 15:36






The default max heap size of 64 MB is from before J2SE 5.0. For J2SE 8.0 information, see "Garbage Collector Ergonomics" at docs.oracle.com/javase/8/docs/technotes/guides/vm/… .

– Andy Thomas
Apr 8 '15 at 15:36














If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

– Steve Steiner
Apr 24 '18 at 20:13





If you landed here because every OOM question is duped to this one, make sure you also check out: stackoverflow.com/questions/299659/… It provides the solution for cleaning up memory references 'just in time' before the OOM. SoftReferences may be the tool that solves your actual problem.

– Steve Steiner
Apr 24 '18 at 20:13












18 Answers
18






active

oldest

votes


















219














Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).



So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.



In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.



If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.



In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.






share|improve this answer




















  • 2





    OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

    – Petr Gladkikh
    Apr 17 '13 at 4:40


















110














Run Java with the command-line option -Xmx, which sets the maximum size of the heap.



See here for details.






share|improve this answer

























  • How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

    – Dr.jacky
    Jan 19 '17 at 7:32











  • Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

    – Arayan Singh
    May 24 '18 at 18:38



















81














You could specify per project how much heap space your project wants



Following is for Eclipse Helios/Juno/Kepler:



Right mouse click on



 Run As - Run Configuration - Arguments - Vm Arguments, 


then add this



-Xmx2048m





share|improve this answer




















  • 1





    hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

    – AndroidRaji
    Nov 24 '12 at 5:49











  • @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

    – CuongHuyTo
    Mar 4 '14 at 10:06


















38














Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.



  1. Use local variables wherever possible.

  2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)

  3. Use a good code system for your program(EX: Using static variables VS non static variables)

  4. Other stuff which could work on your code.

  5. Try to move with multy THREADING





share|improve this answer

























  • This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

    – Ash
    Feb 11 at 5:43











  • @Ash: Yes, fix the core problem instead of looking for plasters.

    – PeakGen
    Feb 12 at 7:59


















31














Big caveat ---- at my office, we were finding that (on some windows machines) we could not allocate more than 512m for Java heap. This turned out to be due to the Kaspersky anti-virus product installed on some of those machines. After uninstalling that AV product, we found we could allocate at least 1.6gb, i.e, -Xmx1600m (m is mandatory other wise it will lead to another error "Too small initial heap") works.



No idea if this happens with other AV products but presumably this is happening because the AV program is reserving a small block of memory in every address space, thereby preventing a single really large allocation.






share|improve this answer
































    20














    VM arguments worked for me in eclipse. If you are using eclipse version 3.4, do the following



    go to Run --> Run Configurations --> then select the project under maven build --> then select the tab "JRE" --> then enter -Xmx1024m.



    Alternatively you could do Run --> Run Configurations --> select the "JRE" tab --> then enter -Xmx1024m



    This should increase the memory heap for all the builds/projects. The above memory size is 1 GB. You can optimize the way you want.






    share|improve this answer
































      16














      Yes, with -Xmx you can configure more memory for your JVM.
      To be sure that you don't leak or waste memory. Take a heap dump and use the Eclipse Memory Analyzer to analyze your memory consumption.






      share|improve this answer

























      • JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

        – Philip Rego
        Jan 15 at 22:28


















      13














      I would like to add recommendations from oracle trouble shooting article.



      Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space




      The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak




      Possible causes:



      1. Simple configuration issue, where the specified heap size is insufficient for the application.


      2. Application is unintentionally holding references to objects, and this prevents the objects from being garbage collected.


      3. Excessive use of finalizers.



      One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time




      After garbage collection, the objects are queued for finalization, which occurs at a later time. finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.



      One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.






      share|improve this answer
































        7














        Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.



        Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.



        export JVM_ARGS="-Xms1024m -Xmx1024m"



        Reference Link






        share|improve this answer




















        • 1





          Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

          – ernesto
          Oct 25 '12 at 10:56


















        7














        By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)



        -Xms<size> set initial Java heap size
        -Xmx<size> set maximum Java heap size
        -Xss<size> set java thread stack size

        -XX:ParallelGCThreads=8
        -XX:+CMSClassUnloadingEnabled
        -XX:InitiatingHeapOccupancyPercent=70
        -XX:+UnlockDiagnosticVMOptions
        -XX:+UseConcMarkSweepGC
        -Xms512m
        -Xmx8192m
        -XX:MaxPermSize=256m (in java 8 optional)


        For example: On linux Platform for production mode preferable settings.



        After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/



        1.create setenv.sh file on folder /opt/tomcat/bin/



         touch /opt/tomcat/bin/setenv.sh


        2.Open and write this params for setting preferable mode.



        nano /opt/tomcat/bin/setenv.sh 

        export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
        export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
        export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
        export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
        export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
        export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
        export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
        export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"


        3.service tomcat restart




        Note that the JVM uses more memory than just the heap. For example
        Java methods, thread stacks and native handles are allocated in memory
        separate from the heap, as well as JVM internal data structures.







        share|improve this answer
































          7














          Follow below steps:



          1. Open catalina.sh from tomcat/bin.



          2. Change JAVA_OPTS to



            JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
            -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
            -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


          3. Restart your tomcat






          share|improve this answer
































            7














            I have faced same problem from java heap size.



            I have two solutions if you are using java 5(1.5).



            1. just install jdk1.6 and go to the preferences of eclipse and set the jre path of jav1 1.6 as you have installed.


            2. Check your VM argument and let it be whatever it is.
              just add one line below of all the arguments present in VM arguments as
              -Xms512m -Xmx512m -XX:MaxPermSize=...m(192m).


            I think it will work...






            share|improve this answer
































              7














              I read somewhere else that you can try - catch java.lang.OutOfMemoryError and on the catch block, you can free all resources that you know might use a lot of memory, close connections and so forth, then do a System.gc() then re-try whatever you were going to do.



              Another way is this although, i don't know whether this would work, but I am currently testing whether it will work on my application.



              The Idea is to do Garbage collection by calling System.gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes.



              //Mimimum acceptable free memory you think your app needs
              long minRunningMemory = (1024*1024);

              Runtime runtime = Runtime.getRuntime();

              if(runtime.freeMemory()<minRunningMemory)
              System.gc();





              share|improve this answer




















              • 6





                In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                – Mike Clark
                Nov 20 '10 at 10:50







              • 5





                @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                – user1014581
                Oct 26 '11 at 12:51


















              7














              If you need to monitor your memory usage at runtime, the java.lang.management package offers MBeans that can be used to monitor the memory pools in your VM (eg, eden space, tenured generation etc), and also garbage collection behaviour.



              The free heap space reported by these MBeans will vary greatly depending on GC behaviour, particularly if your application generates a lot of objects which are later GC-ed. One possible approach is to monitor the free heap space after each full-GC, which you may be able to use to make a decision on freeing up memory by persisting objects.



              Ultimately, your best bet is to limit your memory retention as far as possible whilst performance remains acceptable. As a previous comment noted, memory is always limited, but your app should have a strategy for dealing with memory exhaustion.






              share|improve this answer
































                5














                Note that if you need this in a deployment situation, consider using Java WebStart (with an "ondisk" version, not the network one - possible in Java 6u10 and later) as it allows you to specify the various arguments to the JVM in a cross platform way.



                Otherwise you will need an operating system specific launcher which sets the arguments you need.






                share|improve this answer

























                • Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                  – Thorbjørn Ravn Andersen
                  Dec 31 '18 at 12:33


















                1














                If this issue is happening in Wildfly 8 and JDK1.8,then we need to specify MaxMetaSpace settings instead of PermGen settings.



                For example we need to add below configuration in setenv.sh file of wildfly.
                JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256M"



                For more information, please check Wildfly Heap Issue






                share|improve this answer
































                  1














                  Regarding to netbeans, you could set max heap size to solve the problem.



                  Go to 'Run', then --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up window --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'.






                  share|improve this answer
































                    1














                    If you keep on allocating & keeping references to object, you will fill up any amount of memory you have.



                    One option is to do a transparent file close & open when they switch tabs (you only keep a pointer to the file, and when the user switches tab, you close & clean all the objects... it'll make the file change slower... but...), and maybe keep only 3 or 4 files on memory.



                    Other thing you should do is, when the user opens a file, load it, and intercept any OutOfMemoryError, then (as it is not possible to open the file) close that file, clean its objects and warn the user that he should close unused files.



                    Your idea of dynamically extending virtual memory doesn't solve the issue, for the machine is limited on resources, so you should be carefull & handle memory issues (or at least, be carefull with them).



                    A couple of hints i've seen with memory leaks is:



                    --> Keep on mind that if you put something into a collection and afterwards forget about it, you still have a strong reference to it, so nullify the collection, clean it or do something with it... if not you will find a memory leak difficult to find.



                    --> Maybe, using collections with weak references (weakhashmap...) can help with memory issues, but you must be carefull with it, for you might find that the object you look for has been collected.



                    --> Another idea i've found is to develope a persistent collection that stored on database objects least used and transparently loaded. This would probably be the best approach...






                    share|improve this answer























                      protected by Community Oct 26 '11 at 14:57



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?














                      18 Answers
                      18






                      active

                      oldest

                      votes








                      18 Answers
                      18






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      219














                      Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).



                      So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.



                      In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.



                      If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.



                      In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.






                      share|improve this answer




















                      • 2





                        OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                        – Petr Gladkikh
                        Apr 17 '13 at 4:40















                      219














                      Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).



                      So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.



                      In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.



                      If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.



                      In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.






                      share|improve this answer




















                      • 2





                        OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                        – Petr Gladkikh
                        Apr 17 '13 at 4:40













                      219












                      219








                      219







                      Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).



                      So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.



                      In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.



                      If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.



                      In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.






                      share|improve this answer















                      Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).



                      So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.



                      In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.



                      If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.



                      In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 23 at 8:44









                      Hearen

                      3,03211630




                      3,03211630










                      answered Sep 1 '08 at 1:29









                      Ben ChildsBen Childs

                      3,4171169




                      3,4171169







                      • 2





                        OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                        – Petr Gladkikh
                        Apr 17 '13 at 4:40












                      • 2





                        OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                        – Petr Gladkikh
                        Apr 17 '13 at 4:40







                      2




                      2





                      OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                      – Petr Gladkikh
                      Apr 17 '13 at 4:40





                      OpenJDK and OracleJDK have bundled profiler - jvisualvm. If you want more conveniences I'd suggest commercial Yourkit.

                      – Petr Gladkikh
                      Apr 17 '13 at 4:40













                      110














                      Run Java with the command-line option -Xmx, which sets the maximum size of the heap.



                      See here for details.






                      share|improve this answer

























                      • How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                        – Dr.jacky
                        Jan 19 '17 at 7:32











                      • Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                        – Arayan Singh
                        May 24 '18 at 18:38
















                      110














                      Run Java with the command-line option -Xmx, which sets the maximum size of the heap.



                      See here for details.






                      share|improve this answer

























                      • How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                        – Dr.jacky
                        Jan 19 '17 at 7:32











                      • Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                        – Arayan Singh
                        May 24 '18 at 18:38














                      110












                      110








                      110







                      Run Java with the command-line option -Xmx, which sets the maximum size of the heap.



                      See here for details.






                      share|improve this answer















                      Run Java with the command-line option -Xmx, which sets the maximum size of the heap.



                      See here for details.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 23 at 8:51









                      Hearen

                      3,03211630




                      3,03211630










                      answered Sep 1 '08 at 5:00









                      Dave WebbDave Webb

                      158k49283291




                      158k49283291












                      • How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                        – Dr.jacky
                        Jan 19 '17 at 7:32











                      • Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                        – Arayan Singh
                        May 24 '18 at 18:38


















                      • How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                        – Dr.jacky
                        Jan 19 '17 at 7:32











                      • Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                        – Arayan Singh
                        May 24 '18 at 18:38

















                      How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                      – Dr.jacky
                      Jan 19 '17 at 7:32





                      How to set this parameter for ever? Cause I'm using 'gradlew assemble' command.

                      – Dr.jacky
                      Jan 19 '17 at 7:32













                      Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                      – Arayan Singh
                      May 24 '18 at 18:38






                      Run->Run Configurations->Click on arguments->inside VM arguments type -Xms1g -Xmx2g

                      – Arayan Singh
                      May 24 '18 at 18:38












                      81














                      You could specify per project how much heap space your project wants



                      Following is for Eclipse Helios/Juno/Kepler:



                      Right mouse click on



                       Run As - Run Configuration - Arguments - Vm Arguments, 


                      then add this



                      -Xmx2048m





                      share|improve this answer




















                      • 1





                        hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                        – AndroidRaji
                        Nov 24 '12 at 5:49











                      • @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                        – CuongHuyTo
                        Mar 4 '14 at 10:06















                      81














                      You could specify per project how much heap space your project wants



                      Following is for Eclipse Helios/Juno/Kepler:



                      Right mouse click on



                       Run As - Run Configuration - Arguments - Vm Arguments, 


                      then add this



                      -Xmx2048m





                      share|improve this answer




















                      • 1





                        hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                        – AndroidRaji
                        Nov 24 '12 at 5:49











                      • @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                        – CuongHuyTo
                        Mar 4 '14 at 10:06













                      81












                      81








                      81







                      You could specify per project how much heap space your project wants



                      Following is for Eclipse Helios/Juno/Kepler:



                      Right mouse click on



                       Run As - Run Configuration - Arguments - Vm Arguments, 


                      then add this



                      -Xmx2048m





                      share|improve this answer















                      You could specify per project how much heap space your project wants



                      Following is for Eclipse Helios/Juno/Kepler:



                      Right mouse click on



                       Run As - Run Configuration - Arguments - Vm Arguments, 


                      then add this



                      -Xmx2048m






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 23 at 8:49









                      Hearen

                      3,03211630




                      3,03211630










                      answered Feb 25 '11 at 18:45









                      allenhwkimallenhwkim

                      21.1k1366104




                      21.1k1366104







                      • 1





                        hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                        – AndroidRaji
                        Nov 24 '12 at 5:49











                      • @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                        – CuongHuyTo
                        Mar 4 '14 at 10:06












                      • 1





                        hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                        – AndroidRaji
                        Nov 24 '12 at 5:49











                      • @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                        – CuongHuyTo
                        Mar 4 '14 at 10:06







                      1




                      1





                      hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                      – AndroidRaji
                      Nov 24 '12 at 5:49





                      hi bighostkim and cuongHuyTo, where is "Arguments"..i can able to see upto Run Configuration. Please tel me. My need to download and store nearly 2000 contacts from gmail. It crash due to out of memory exception

                      – AndroidRaji
                      Nov 24 '12 at 5:49













                      @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                      – CuongHuyTo
                      Mar 4 '14 at 10:06





                      @AndroiRaji: you right click your mouse onto the Java class that has a runnable main (that is "public static void main(String[] args)"), then choose Run As - Run Configuration. Then "Arguments" is the tab right after the Main (you see the tabs Main, Arguments, JRE, Classpath, Source, Environment, Common).

                      – CuongHuyTo
                      Mar 4 '14 at 10:06











                      38














                      Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.



                      1. Use local variables wherever possible.

                      2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)

                      3. Use a good code system for your program(EX: Using static variables VS non static variables)

                      4. Other stuff which could work on your code.

                      5. Try to move with multy THREADING





                      share|improve this answer

























                      • This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                        – Ash
                        Feb 11 at 5:43











                      • @Ash: Yes, fix the core problem instead of looking for plasters.

                        – PeakGen
                        Feb 12 at 7:59















                      38














                      Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.



                      1. Use local variables wherever possible.

                      2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)

                      3. Use a good code system for your program(EX: Using static variables VS non static variables)

                      4. Other stuff which could work on your code.

                      5. Try to move with multy THREADING





                      share|improve this answer

























                      • This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                        – Ash
                        Feb 11 at 5:43











                      • @Ash: Yes, fix the core problem instead of looking for plasters.

                        – PeakGen
                        Feb 12 at 7:59













                      38












                      38








                      38







                      Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.



                      1. Use local variables wherever possible.

                      2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)

                      3. Use a good code system for your program(EX: Using static variables VS non static variables)

                      4. Other stuff which could work on your code.

                      5. Try to move with multy THREADING





                      share|improve this answer















                      Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.



                      1. Use local variables wherever possible.

                      2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)

                      3. Use a good code system for your program(EX: Using static variables VS non static variables)

                      4. Other stuff which could work on your code.

                      5. Try to move with multy THREADING






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 24 at 6:41

























                      answered Feb 1 '14 at 5:10









                      PeakGenPeakGen

                      8,39649161301




                      8,39649161301












                      • This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                        – Ash
                        Feb 11 at 5:43











                      • @Ash: Yes, fix the core problem instead of looking for plasters.

                        – PeakGen
                        Feb 12 at 7:59

















                      • This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                        – Ash
                        Feb 11 at 5:43











                      • @Ash: Yes, fix the core problem instead of looking for plasters.

                        – PeakGen
                        Feb 12 at 7:59
















                      This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                      – Ash
                      Feb 11 at 5:43





                      This is so true. I am trying to fix one issue where I'm getting OOM on AWT thread but if I use different new thread, I am not getting OOM issue. All I can find online is increase heap size for AWT thread.

                      – Ash
                      Feb 11 at 5:43













                      @Ash: Yes, fix the core problem instead of looking for plasters.

                      – PeakGen
                      Feb 12 at 7:59





                      @Ash: Yes, fix the core problem instead of looking for plasters.

                      – PeakGen
                      Feb 12 at 7:59











                      31














                      Big caveat ---- at my office, we were finding that (on some windows machines) we could not allocate more than 512m for Java heap. This turned out to be due to the Kaspersky anti-virus product installed on some of those machines. After uninstalling that AV product, we found we could allocate at least 1.6gb, i.e, -Xmx1600m (m is mandatory other wise it will lead to another error "Too small initial heap") works.



                      No idea if this happens with other AV products but presumably this is happening because the AV program is reserving a small block of memory in every address space, thereby preventing a single really large allocation.






                      share|improve this answer





























                        31














                        Big caveat ---- at my office, we were finding that (on some windows machines) we could not allocate more than 512m for Java heap. This turned out to be due to the Kaspersky anti-virus product installed on some of those machines. After uninstalling that AV product, we found we could allocate at least 1.6gb, i.e, -Xmx1600m (m is mandatory other wise it will lead to another error "Too small initial heap") works.



                        No idea if this happens with other AV products but presumably this is happening because the AV program is reserving a small block of memory in every address space, thereby preventing a single really large allocation.






                        share|improve this answer



























                          31












                          31








                          31







                          Big caveat ---- at my office, we were finding that (on some windows machines) we could not allocate more than 512m for Java heap. This turned out to be due to the Kaspersky anti-virus product installed on some of those machines. After uninstalling that AV product, we found we could allocate at least 1.6gb, i.e, -Xmx1600m (m is mandatory other wise it will lead to another error "Too small initial heap") works.



                          No idea if this happens with other AV products but presumably this is happening because the AV program is reserving a small block of memory in every address space, thereby preventing a single really large allocation.






                          share|improve this answer















                          Big caveat ---- at my office, we were finding that (on some windows machines) we could not allocate more than 512m for Java heap. This turned out to be due to the Kaspersky anti-virus product installed on some of those machines. After uninstalling that AV product, we found we could allocate at least 1.6gb, i.e, -Xmx1600m (m is mandatory other wise it will lead to another error "Too small initial heap") works.



                          No idea if this happens with other AV products but presumably this is happening because the AV program is reserving a small block of memory in every address space, thereby preventing a single really large allocation.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 23 at 8:46









                          Hearen

                          3,03211630




                          3,03211630










                          answered Apr 14 '09 at 13:34









                          DavidDavid

                          2,63442435




                          2,63442435





















                              20














                              VM arguments worked for me in eclipse. If you are using eclipse version 3.4, do the following



                              go to Run --> Run Configurations --> then select the project under maven build --> then select the tab "JRE" --> then enter -Xmx1024m.



                              Alternatively you could do Run --> Run Configurations --> select the "JRE" tab --> then enter -Xmx1024m



                              This should increase the memory heap for all the builds/projects. The above memory size is 1 GB. You can optimize the way you want.






                              share|improve this answer





























                                20














                                VM arguments worked for me in eclipse. If you are using eclipse version 3.4, do the following



                                go to Run --> Run Configurations --> then select the project under maven build --> then select the tab "JRE" --> then enter -Xmx1024m.



                                Alternatively you could do Run --> Run Configurations --> select the "JRE" tab --> then enter -Xmx1024m



                                This should increase the memory heap for all the builds/projects. The above memory size is 1 GB. You can optimize the way you want.






                                share|improve this answer



























                                  20












                                  20








                                  20







                                  VM arguments worked for me in eclipse. If you are using eclipse version 3.4, do the following



                                  go to Run --> Run Configurations --> then select the project under maven build --> then select the tab "JRE" --> then enter -Xmx1024m.



                                  Alternatively you could do Run --> Run Configurations --> select the "JRE" tab --> then enter -Xmx1024m



                                  This should increase the memory heap for all the builds/projects. The above memory size is 1 GB. You can optimize the way you want.






                                  share|improve this answer















                                  VM arguments worked for me in eclipse. If you are using eclipse version 3.4, do the following



                                  go to Run --> Run Configurations --> then select the project under maven build --> then select the tab "JRE" --> then enter -Xmx1024m.



                                  Alternatively you could do Run --> Run Configurations --> select the "JRE" tab --> then enter -Xmx1024m



                                  This should increase the memory heap for all the builds/projects. The above memory size is 1 GB. You can optimize the way you want.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Mar 23 at 8:50









                                  Hearen

                                  3,03211630




                                  3,03211630










                                  answered Jun 9 '11 at 20:48









                                  loveallloveall

                                  20122




                                  20122





















                                      16














                                      Yes, with -Xmx you can configure more memory for your JVM.
                                      To be sure that you don't leak or waste memory. Take a heap dump and use the Eclipse Memory Analyzer to analyze your memory consumption.






                                      share|improve this answer

























                                      • JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                        – Philip Rego
                                        Jan 15 at 22:28















                                      16














                                      Yes, with -Xmx you can configure more memory for your JVM.
                                      To be sure that you don't leak or waste memory. Take a heap dump and use the Eclipse Memory Analyzer to analyze your memory consumption.






                                      share|improve this answer

























                                      • JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                        – Philip Rego
                                        Jan 15 at 22:28













                                      16












                                      16








                                      16







                                      Yes, with -Xmx you can configure more memory for your JVM.
                                      To be sure that you don't leak or waste memory. Take a heap dump and use the Eclipse Memory Analyzer to analyze your memory consumption.






                                      share|improve this answer















                                      Yes, with -Xmx you can configure more memory for your JVM.
                                      To be sure that you don't leak or waste memory. Take a heap dump and use the Eclipse Memory Analyzer to analyze your memory consumption.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 23 at 8:53









                                      Hearen

                                      3,03211630




                                      3,03211630










                                      answered Oct 9 '08 at 8:22









                                      kohlermkohlerm

                                      2,28811417




                                      2,28811417












                                      • JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                        – Philip Rego
                                        Jan 15 at 22:28

















                                      • JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                        – Philip Rego
                                        Jan 15 at 22:28
















                                      JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                      – Philip Rego
                                      Jan 15 at 22:28





                                      JVMJ9VM007E Command-line option unrecognised: -Xmx Could not create the Java virtual machine. Downvote

                                      – Philip Rego
                                      Jan 15 at 22:28











                                      13














                                      I would like to add recommendations from oracle trouble shooting article.



                                      Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space




                                      The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak




                                      Possible causes:



                                      1. Simple configuration issue, where the specified heap size is insufficient for the application.


                                      2. Application is unintentionally holding references to objects, and this prevents the objects from being garbage collected.


                                      3. Excessive use of finalizers.



                                      One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time




                                      After garbage collection, the objects are queued for finalization, which occurs at a later time. finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.



                                      One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.






                                      share|improve this answer





























                                        13














                                        I would like to add recommendations from oracle trouble shooting article.



                                        Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space




                                        The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak




                                        Possible causes:



                                        1. Simple configuration issue, where the specified heap size is insufficient for the application.


                                        2. Application is unintentionally holding references to objects, and this prevents the objects from being garbage collected.


                                        3. Excessive use of finalizers.



                                        One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time




                                        After garbage collection, the objects are queued for finalization, which occurs at a later time. finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.



                                        One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.






                                        share|improve this answer



























                                          13












                                          13








                                          13







                                          I would like to add recommendations from oracle trouble shooting article.



                                          Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space




                                          The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak




                                          Possible causes:



                                          1. Simple configuration issue, where the specified heap size is insufficient for the application.


                                          2. Application is unintentionally holding references to objects, and this prevents the objects from being garbage collected.


                                          3. Excessive use of finalizers.



                                          One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time




                                          After garbage collection, the objects are queued for finalization, which occurs at a later time. finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.



                                          One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.






                                          share|improve this answer















                                          I would like to add recommendations from oracle trouble shooting article.



                                          Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space




                                          The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak




                                          Possible causes:



                                          1. Simple configuration issue, where the specified heap size is insufficient for the application.


                                          2. Application is unintentionally holding references to objects, and this prevents the objects from being garbage collected.


                                          3. Excessive use of finalizers.



                                          One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time




                                          After garbage collection, the objects are queued for finalization, which occurs at a later time. finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.



                                          One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Mar 23 at 8:47









                                          Hearen

                                          3,03211630




                                          3,03211630










                                          answered Mar 22 '16 at 18:35









                                          Ravindra babuRavindra babu

                                          31.2k6169144




                                          31.2k6169144





















                                              7














                                              Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.



                                              Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.



                                              export JVM_ARGS="-Xms1024m -Xmx1024m"



                                              Reference Link






                                              share|improve this answer




















                                              • 1





                                                Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                                – ernesto
                                                Oct 25 '12 at 10:56















                                              7














                                              Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.



                                              Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.



                                              export JVM_ARGS="-Xms1024m -Xmx1024m"



                                              Reference Link






                                              share|improve this answer




















                                              • 1





                                                Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                                – ernesto
                                                Oct 25 '12 at 10:56













                                              7












                                              7








                                              7







                                              Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.



                                              Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.



                                              export JVM_ARGS="-Xms1024m -Xmx1024m"



                                              Reference Link






                                              share|improve this answer















                                              Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.



                                              Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.



                                              export JVM_ARGS="-Xms1024m -Xmx1024m"



                                              Reference Link







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jun 20 '12 at 10:34









                                              mtk

                                              7,556105494




                                              7,556105494










                                              answered Sep 4 '11 at 5:05









                                              chauksseychaukssey

                                              7111




                                              7111







                                              • 1





                                                Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                                – ernesto
                                                Oct 25 '12 at 10:56












                                              • 1





                                                Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                                – ernesto
                                                Oct 25 '12 at 10:56







                                              1




                                              1





                                              Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                              – ernesto
                                              Oct 25 '12 at 10:56





                                              Any idea why we need to keep them in 1:1 or 1:1.5 ratio?

                                              – ernesto
                                              Oct 25 '12 at 10:56











                                              7














                                              By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)



                                              -Xms<size> set initial Java heap size
                                              -Xmx<size> set maximum Java heap size
                                              -Xss<size> set java thread stack size

                                              -XX:ParallelGCThreads=8
                                              -XX:+CMSClassUnloadingEnabled
                                              -XX:InitiatingHeapOccupancyPercent=70
                                              -XX:+UnlockDiagnosticVMOptions
                                              -XX:+UseConcMarkSweepGC
                                              -Xms512m
                                              -Xmx8192m
                                              -XX:MaxPermSize=256m (in java 8 optional)


                                              For example: On linux Platform for production mode preferable settings.



                                              After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/



                                              1.create setenv.sh file on folder /opt/tomcat/bin/



                                               touch /opt/tomcat/bin/setenv.sh


                                              2.Open and write this params for setting preferable mode.



                                              nano /opt/tomcat/bin/setenv.sh 

                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
                                              export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
                                              export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
                                              export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"


                                              3.service tomcat restart




                                              Note that the JVM uses more memory than just the heap. For example
                                              Java methods, thread stacks and native handles are allocated in memory
                                              separate from the heap, as well as JVM internal data structures.







                                              share|improve this answer





























                                                7














                                                By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)



                                                -Xms<size> set initial Java heap size
                                                -Xmx<size> set maximum Java heap size
                                                -Xss<size> set java thread stack size

                                                -XX:ParallelGCThreads=8
                                                -XX:+CMSClassUnloadingEnabled
                                                -XX:InitiatingHeapOccupancyPercent=70
                                                -XX:+UnlockDiagnosticVMOptions
                                                -XX:+UseConcMarkSweepGC
                                                -Xms512m
                                                -Xmx8192m
                                                -XX:MaxPermSize=256m (in java 8 optional)


                                                For example: On linux Platform for production mode preferable settings.



                                                After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/



                                                1.create setenv.sh file on folder /opt/tomcat/bin/



                                                 touch /opt/tomcat/bin/setenv.sh


                                                2.Open and write this params for setting preferable mode.



                                                nano /opt/tomcat/bin/setenv.sh 

                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
                                                export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
                                                export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
                                                export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"


                                                3.service tomcat restart




                                                Note that the JVM uses more memory than just the heap. For example
                                                Java methods, thread stacks and native handles are allocated in memory
                                                separate from the heap, as well as JVM internal data structures.







                                                share|improve this answer



























                                                  7












                                                  7








                                                  7







                                                  By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)



                                                  -Xms<size> set initial Java heap size
                                                  -Xmx<size> set maximum Java heap size
                                                  -Xss<size> set java thread stack size

                                                  -XX:ParallelGCThreads=8
                                                  -XX:+CMSClassUnloadingEnabled
                                                  -XX:InitiatingHeapOccupancyPercent=70
                                                  -XX:+UnlockDiagnosticVMOptions
                                                  -XX:+UseConcMarkSweepGC
                                                  -Xms512m
                                                  -Xmx8192m
                                                  -XX:MaxPermSize=256m (in java 8 optional)


                                                  For example: On linux Platform for production mode preferable settings.



                                                  After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/



                                                  1.create setenv.sh file on folder /opt/tomcat/bin/



                                                   touch /opt/tomcat/bin/setenv.sh


                                                  2.Open and write this params for setting preferable mode.



                                                  nano /opt/tomcat/bin/setenv.sh 

                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"


                                                  3.service tomcat restart




                                                  Note that the JVM uses more memory than just the heap. For example
                                                  Java methods, thread stacks and native handles are allocated in memory
                                                  separate from the heap, as well as JVM internal data structures.







                                                  share|improve this answer















                                                  By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)



                                                  -Xms<size> set initial Java heap size
                                                  -Xmx<size> set maximum Java heap size
                                                  -Xss<size> set java thread stack size

                                                  -XX:ParallelGCThreads=8
                                                  -XX:+CMSClassUnloadingEnabled
                                                  -XX:InitiatingHeapOccupancyPercent=70
                                                  -XX:+UnlockDiagnosticVMOptions
                                                  -XX:+UseConcMarkSweepGC
                                                  -Xms512m
                                                  -Xmx8192m
                                                  -XX:MaxPermSize=256m (in java 8 optional)


                                                  For example: On linux Platform for production mode preferable settings.



                                                  After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/



                                                  1.create setenv.sh file on folder /opt/tomcat/bin/



                                                   touch /opt/tomcat/bin/setenv.sh


                                                  2.Open and write this params for setting preferable mode.



                                                  nano /opt/tomcat/bin/setenv.sh 

                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
                                                  export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"


                                                  3.service tomcat restart




                                                  Note that the JVM uses more memory than just the heap. For example
                                                  Java methods, thread stacks and native handles are allocated in memory
                                                  separate from the heap, as well as JVM internal data structures.








                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Mar 23 at 8:45









                                                  Hearen

                                                  3,03211630




                                                  3,03211630










                                                  answered May 26 '16 at 12:00









                                                  MusaMusa

                                                  1,2481417




                                                  1,2481417





















                                                      7














                                                      Follow below steps:



                                                      1. Open catalina.sh from tomcat/bin.



                                                      2. Change JAVA_OPTS to



                                                        JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
                                                        -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
                                                        -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


                                                      3. Restart your tomcat






                                                      share|improve this answer





























                                                        7














                                                        Follow below steps:



                                                        1. Open catalina.sh from tomcat/bin.



                                                        2. Change JAVA_OPTS to



                                                          JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
                                                          -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
                                                          -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


                                                        3. Restart your tomcat






                                                        share|improve this answer



























                                                          7












                                                          7








                                                          7







                                                          Follow below steps:



                                                          1. Open catalina.sh from tomcat/bin.



                                                          2. Change JAVA_OPTS to



                                                            JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
                                                            -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
                                                            -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


                                                          3. Restart your tomcat






                                                          share|improve this answer















                                                          Follow below steps:



                                                          1. Open catalina.sh from tomcat/bin.



                                                          2. Change JAVA_OPTS to



                                                            JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
                                                            -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
                                                            -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


                                                          3. Restart your tomcat







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Mar 23 at 8:46









                                                          Hearen

                                                          3,03211630




                                                          3,03211630










                                                          answered Oct 7 '13 at 10:56









                                                          Pradip BhattPradip Bhatt

                                                          605813




                                                          605813





















                                                              7














                                                              I have faced same problem from java heap size.



                                                              I have two solutions if you are using java 5(1.5).



                                                              1. just install jdk1.6 and go to the preferences of eclipse and set the jre path of jav1 1.6 as you have installed.


                                                              2. Check your VM argument and let it be whatever it is.
                                                                just add one line below of all the arguments present in VM arguments as
                                                                -Xms512m -Xmx512m -XX:MaxPermSize=...m(192m).


                                                              I think it will work...






                                                              share|improve this answer





























                                                                7














                                                                I have faced same problem from java heap size.



                                                                I have two solutions if you are using java 5(1.5).



                                                                1. just install jdk1.6 and go to the preferences of eclipse and set the jre path of jav1 1.6 as you have installed.


                                                                2. Check your VM argument and let it be whatever it is.
                                                                  just add one line below of all the arguments present in VM arguments as
                                                                  -Xms512m -Xmx512m -XX:MaxPermSize=...m(192m).


                                                                I think it will work...






                                                                share|improve this answer



























                                                                  7












                                                                  7








                                                                  7







                                                                  I have faced same problem from java heap size.



                                                                  I have two solutions if you are using java 5(1.5).



                                                                  1. just install jdk1.6 and go to the preferences of eclipse and set the jre path of jav1 1.6 as you have installed.


                                                                  2. Check your VM argument and let it be whatever it is.
                                                                    just add one line below of all the arguments present in VM arguments as
                                                                    -Xms512m -Xmx512m -XX:MaxPermSize=...m(192m).


                                                                  I think it will work...






                                                                  share|improve this answer















                                                                  I have faced same problem from java heap size.



                                                                  I have two solutions if you are using java 5(1.5).



                                                                  1. just install jdk1.6 and go to the preferences of eclipse and set the jre path of jav1 1.6 as you have installed.


                                                                  2. Check your VM argument and let it be whatever it is.
                                                                    just add one line below of all the arguments present in VM arguments as
                                                                    -Xms512m -Xmx512m -XX:MaxPermSize=...m(192m).


                                                                  I think it will work...







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Mar 23 at 8:51









                                                                  Hearen

                                                                  3,03211630




                                                                  3,03211630










                                                                  answered May 10 '11 at 18:48









                                                                  Soumya Sandeep MohantySoumya Sandeep Mohanty

                                                                  7111




                                                                  7111





















                                                                      7














                                                                      I read somewhere else that you can try - catch java.lang.OutOfMemoryError and on the catch block, you can free all resources that you know might use a lot of memory, close connections and so forth, then do a System.gc() then re-try whatever you were going to do.



                                                                      Another way is this although, i don't know whether this would work, but I am currently testing whether it will work on my application.



                                                                      The Idea is to do Garbage collection by calling System.gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes.



                                                                      //Mimimum acceptable free memory you think your app needs
                                                                      long minRunningMemory = (1024*1024);

                                                                      Runtime runtime = Runtime.getRuntime();

                                                                      if(runtime.freeMemory()<minRunningMemory)
                                                                      System.gc();





                                                                      share|improve this answer




















                                                                      • 6





                                                                        In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                        – Mike Clark
                                                                        Nov 20 '10 at 10:50







                                                                      • 5





                                                                        @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                        – user1014581
                                                                        Oct 26 '11 at 12:51















                                                                      7














                                                                      I read somewhere else that you can try - catch java.lang.OutOfMemoryError and on the catch block, you can free all resources that you know might use a lot of memory, close connections and so forth, then do a System.gc() then re-try whatever you were going to do.



                                                                      Another way is this although, i don't know whether this would work, but I am currently testing whether it will work on my application.



                                                                      The Idea is to do Garbage collection by calling System.gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes.



                                                                      //Mimimum acceptable free memory you think your app needs
                                                                      long minRunningMemory = (1024*1024);

                                                                      Runtime runtime = Runtime.getRuntime();

                                                                      if(runtime.freeMemory()<minRunningMemory)
                                                                      System.gc();





                                                                      share|improve this answer




















                                                                      • 6





                                                                        In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                        – Mike Clark
                                                                        Nov 20 '10 at 10:50







                                                                      • 5





                                                                        @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                        – user1014581
                                                                        Oct 26 '11 at 12:51













                                                                      7












                                                                      7








                                                                      7







                                                                      I read somewhere else that you can try - catch java.lang.OutOfMemoryError and on the catch block, you can free all resources that you know might use a lot of memory, close connections and so forth, then do a System.gc() then re-try whatever you were going to do.



                                                                      Another way is this although, i don't know whether this would work, but I am currently testing whether it will work on my application.



                                                                      The Idea is to do Garbage collection by calling System.gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes.



                                                                      //Mimimum acceptable free memory you think your app needs
                                                                      long minRunningMemory = (1024*1024);

                                                                      Runtime runtime = Runtime.getRuntime();

                                                                      if(runtime.freeMemory()<minRunningMemory)
                                                                      System.gc();





                                                                      share|improve this answer















                                                                      I read somewhere else that you can try - catch java.lang.OutOfMemoryError and on the catch block, you can free all resources that you know might use a lot of memory, close connections and so forth, then do a System.gc() then re-try whatever you were going to do.



                                                                      Another way is this although, i don't know whether this would work, but I am currently testing whether it will work on my application.



                                                                      The Idea is to do Garbage collection by calling System.gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes.



                                                                      //Mimimum acceptable free memory you think your app needs
                                                                      long minRunningMemory = (1024*1024);

                                                                      Runtime runtime = Runtime.getRuntime();

                                                                      if(runtime.freeMemory()<minRunningMemory)
                                                                      System.gc();






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Mar 23 at 8:52









                                                                      Hearen

                                                                      3,03211630




                                                                      3,03211630










                                                                      answered Aug 17 '10 at 7:30









                                                                      mwangimwangi

                                                                      1,3231722




                                                                      1,3231722







                                                                      • 6





                                                                        In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                        – Mike Clark
                                                                        Nov 20 '10 at 10:50







                                                                      • 5





                                                                        @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                        – user1014581
                                                                        Oct 26 '11 at 12:51












                                                                      • 6





                                                                        In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                        – Mike Clark
                                                                        Nov 20 '10 at 10:50







                                                                      • 5





                                                                        @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                        – user1014581
                                                                        Oct 26 '11 at 12:51







                                                                      6




                                                                      6





                                                                      In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                      – Mike Clark
                                                                      Nov 20 '10 at 10:50






                                                                      In general I think the JVM will prefer to Garbage Collect (GC) rather than throw an OutOfMemoryError. Explicitly calling System.gc() after OutOfMemoryError might possibly help on some VMs/configurations, but I would not expect it to work very well in the general case. However, dropping unnecessary object references would definitely help in almost all cases.

                                                                      – Mike Clark
                                                                      Nov 20 '10 at 10:50





                                                                      5




                                                                      5





                                                                      @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                      – user1014581
                                                                      Oct 26 '11 at 12:51





                                                                      @mwangi Calling System.gc() directly from the code is generally a bad idea. It's just a suggestion to JVM that GC should be performed, but there's absolutely no guarantee that it will be performed.

                                                                      – user1014581
                                                                      Oct 26 '11 at 12:51











                                                                      7














                                                                      If you need to monitor your memory usage at runtime, the java.lang.management package offers MBeans that can be used to monitor the memory pools in your VM (eg, eden space, tenured generation etc), and also garbage collection behaviour.



                                                                      The free heap space reported by these MBeans will vary greatly depending on GC behaviour, particularly if your application generates a lot of objects which are later GC-ed. One possible approach is to monitor the free heap space after each full-GC, which you may be able to use to make a decision on freeing up memory by persisting objects.



                                                                      Ultimately, your best bet is to limit your memory retention as far as possible whilst performance remains acceptable. As a previous comment noted, memory is always limited, but your app should have a strategy for dealing with memory exhaustion.






                                                                      share|improve this answer





























                                                                        7














                                                                        If you need to monitor your memory usage at runtime, the java.lang.management package offers MBeans that can be used to monitor the memory pools in your VM (eg, eden space, tenured generation etc), and also garbage collection behaviour.



                                                                        The free heap space reported by these MBeans will vary greatly depending on GC behaviour, particularly if your application generates a lot of objects which are later GC-ed. One possible approach is to monitor the free heap space after each full-GC, which you may be able to use to make a decision on freeing up memory by persisting objects.



                                                                        Ultimately, your best bet is to limit your memory retention as far as possible whilst performance remains acceptable. As a previous comment noted, memory is always limited, but your app should have a strategy for dealing with memory exhaustion.






                                                                        share|improve this answer



























                                                                          7












                                                                          7








                                                                          7







                                                                          If you need to monitor your memory usage at runtime, the java.lang.management package offers MBeans that can be used to monitor the memory pools in your VM (eg, eden space, tenured generation etc), and also garbage collection behaviour.



                                                                          The free heap space reported by these MBeans will vary greatly depending on GC behaviour, particularly if your application generates a lot of objects which are later GC-ed. One possible approach is to monitor the free heap space after each full-GC, which you may be able to use to make a decision on freeing up memory by persisting objects.



                                                                          Ultimately, your best bet is to limit your memory retention as far as possible whilst performance remains acceptable. As a previous comment noted, memory is always limited, but your app should have a strategy for dealing with memory exhaustion.






                                                                          share|improve this answer















                                                                          If you need to monitor your memory usage at runtime, the java.lang.management package offers MBeans that can be used to monitor the memory pools in your VM (eg, eden space, tenured generation etc), and also garbage collection behaviour.



                                                                          The free heap space reported by these MBeans will vary greatly depending on GC behaviour, particularly if your application generates a lot of objects which are later GC-ed. One possible approach is to monitor the free heap space after each full-GC, which you may be able to use to make a decision on freeing up memory by persisting objects.



                                                                          Ultimately, your best bet is to limit your memory retention as far as possible whilst performance remains acceptable. As a previous comment noted, memory is always limited, but your app should have a strategy for dealing with memory exhaustion.







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Mar 23 at 8:53









                                                                          Hearen

                                                                          3,03211630




                                                                          3,03211630










                                                                          answered Oct 9 '08 at 12:04









                                                                          LeighLeigh

                                                                          3,77812217




                                                                          3,77812217





















                                                                              5














                                                                              Note that if you need this in a deployment situation, consider using Java WebStart (with an "ondisk" version, not the network one - possible in Java 6u10 and later) as it allows you to specify the various arguments to the JVM in a cross platform way.



                                                                              Otherwise you will need an operating system specific launcher which sets the arguments you need.






                                                                              share|improve this answer

























                                                                              • Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                                – Thorbjørn Ravn Andersen
                                                                                Dec 31 '18 at 12:33















                                                                              5














                                                                              Note that if you need this in a deployment situation, consider using Java WebStart (with an "ondisk" version, not the network one - possible in Java 6u10 and later) as it allows you to specify the various arguments to the JVM in a cross platform way.



                                                                              Otherwise you will need an operating system specific launcher which sets the arguments you need.






                                                                              share|improve this answer

























                                                                              • Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                                – Thorbjørn Ravn Andersen
                                                                                Dec 31 '18 at 12:33













                                                                              5












                                                                              5








                                                                              5







                                                                              Note that if you need this in a deployment situation, consider using Java WebStart (with an "ondisk" version, not the network one - possible in Java 6u10 and later) as it allows you to specify the various arguments to the JVM in a cross platform way.



                                                                              Otherwise you will need an operating system specific launcher which sets the arguments you need.






                                                                              share|improve this answer















                                                                              Note that if you need this in a deployment situation, consider using Java WebStart (with an "ondisk" version, not the network one - possible in Java 6u10 and later) as it allows you to specify the various arguments to the JVM in a cross platform way.



                                                                              Otherwise you will need an operating system specific launcher which sets the arguments you need.







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Mar 23 at 8:52









                                                                              Hearen

                                                                              3,03211630




                                                                              3,03211630










                                                                              answered May 19 '09 at 4:45









                                                                              Thorbjørn Ravn AndersenThorbjørn Ravn Andersen

                                                                              58.2k25152293




                                                                              58.2k25152293












                                                                              • Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                                – Thorbjørn Ravn Andersen
                                                                                Dec 31 '18 at 12:33

















                                                                              • Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                                – Thorbjørn Ravn Andersen
                                                                                Dec 31 '18 at 12:33
















                                                                              Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                              – Thorbjørn Ravn Andersen
                                                                              Dec 31 '18 at 12:33





                                                                              Java WebStart is being phased out. I am not yet aware of a suitable replacement.

                                                                              – Thorbjørn Ravn Andersen
                                                                              Dec 31 '18 at 12:33











                                                                              1














                                                                              If this issue is happening in Wildfly 8 and JDK1.8,then we need to specify MaxMetaSpace settings instead of PermGen settings.



                                                                              For example we need to add below configuration in setenv.sh file of wildfly.
                                                                              JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256M"



                                                                              For more information, please check Wildfly Heap Issue






                                                                              share|improve this answer





























                                                                                1














                                                                                If this issue is happening in Wildfly 8 and JDK1.8,then we need to specify MaxMetaSpace settings instead of PermGen settings.



                                                                                For example we need to add below configuration in setenv.sh file of wildfly.
                                                                                JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256M"



                                                                                For more information, please check Wildfly Heap Issue






                                                                                share|improve this answer



























                                                                                  1












                                                                                  1








                                                                                  1







                                                                                  If this issue is happening in Wildfly 8 and JDK1.8,then we need to specify MaxMetaSpace settings instead of PermGen settings.



                                                                                  For example we need to add below configuration in setenv.sh file of wildfly.
                                                                                  JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256M"



                                                                                  For more information, please check Wildfly Heap Issue






                                                                                  share|improve this answer















                                                                                  If this issue is happening in Wildfly 8 and JDK1.8,then we need to specify MaxMetaSpace settings instead of PermGen settings.



                                                                                  For example we need to add below configuration in setenv.sh file of wildfly.
                                                                                  JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256M"



                                                                                  For more information, please check Wildfly Heap Issue







                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited Aug 28 '17 at 5:10









                                                                                  aristotll

                                                                                  4,59521841




                                                                                  4,59521841










                                                                                  answered Apr 4 '17 at 15:52









                                                                                  satishsatish

                                                                                  92353




                                                                                  92353





















                                                                                      1














                                                                                      Regarding to netbeans, you could set max heap size to solve the problem.



                                                                                      Go to 'Run', then --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up window --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'.






                                                                                      share|improve this answer





























                                                                                        1














                                                                                        Regarding to netbeans, you could set max heap size to solve the problem.



                                                                                        Go to 'Run', then --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up window --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'.






                                                                                        share|improve this answer



























                                                                                          1












                                                                                          1








                                                                                          1







                                                                                          Regarding to netbeans, you could set max heap size to solve the problem.



                                                                                          Go to 'Run', then --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up window --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'.






                                                                                          share|improve this answer















                                                                                          Regarding to netbeans, you could set max heap size to solve the problem.



                                                                                          Go to 'Run', then --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up window --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'.







                                                                                          share|improve this answer














                                                                                          share|improve this answer



                                                                                          share|improve this answer








                                                                                          edited Mar 23 at 8:48









                                                                                          Hearen

                                                                                          3,03211630




                                                                                          3,03211630










                                                                                          answered Jun 12 '17 at 22:56









                                                                                          XiaogangXiaogang

                                                                                          572




                                                                                          572





















                                                                                              1














                                                                                              If you keep on allocating & keeping references to object, you will fill up any amount of memory you have.



                                                                                              One option is to do a transparent file close & open when they switch tabs (you only keep a pointer to the file, and when the user switches tab, you close & clean all the objects... it'll make the file change slower... but...), and maybe keep only 3 or 4 files on memory.



                                                                                              Other thing you should do is, when the user opens a file, load it, and intercept any OutOfMemoryError, then (as it is not possible to open the file) close that file, clean its objects and warn the user that he should close unused files.



                                                                                              Your idea of dynamically extending virtual memory doesn't solve the issue, for the machine is limited on resources, so you should be carefull & handle memory issues (or at least, be carefull with them).



                                                                                              A couple of hints i've seen with memory leaks is:



                                                                                              --> Keep on mind that if you put something into a collection and afterwards forget about it, you still have a strong reference to it, so nullify the collection, clean it or do something with it... if not you will find a memory leak difficult to find.



                                                                                              --> Maybe, using collections with weak references (weakhashmap...) can help with memory issues, but you must be carefull with it, for you might find that the object you look for has been collected.



                                                                                              --> Another idea i've found is to develope a persistent collection that stored on database objects least used and transparently loaded. This would probably be the best approach...






                                                                                              share|improve this answer





























                                                                                                1














                                                                                                If you keep on allocating & keeping references to object, you will fill up any amount of memory you have.



                                                                                                One option is to do a transparent file close & open when they switch tabs (you only keep a pointer to the file, and when the user switches tab, you close & clean all the objects... it'll make the file change slower... but...), and maybe keep only 3 or 4 files on memory.



                                                                                                Other thing you should do is, when the user opens a file, load it, and intercept any OutOfMemoryError, then (as it is not possible to open the file) close that file, clean its objects and warn the user that he should close unused files.



                                                                                                Your idea of dynamically extending virtual memory doesn't solve the issue, for the machine is limited on resources, so you should be carefull & handle memory issues (or at least, be carefull with them).



                                                                                                A couple of hints i've seen with memory leaks is:



                                                                                                --> Keep on mind that if you put something into a collection and afterwards forget about it, you still have a strong reference to it, so nullify the collection, clean it or do something with it... if not you will find a memory leak difficult to find.



                                                                                                --> Maybe, using collections with weak references (weakhashmap...) can help with memory issues, but you must be carefull with it, for you might find that the object you look for has been collected.



                                                                                                --> Another idea i've found is to develope a persistent collection that stored on database objects least used and transparently loaded. This would probably be the best approach...






                                                                                                share|improve this answer



























                                                                                                  1












                                                                                                  1








                                                                                                  1







                                                                                                  If you keep on allocating & keeping references to object, you will fill up any amount of memory you have.



                                                                                                  One option is to do a transparent file close & open when they switch tabs (you only keep a pointer to the file, and when the user switches tab, you close & clean all the objects... it'll make the file change slower... but...), and maybe keep only 3 or 4 files on memory.



                                                                                                  Other thing you should do is, when the user opens a file, load it, and intercept any OutOfMemoryError, then (as it is not possible to open the file) close that file, clean its objects and warn the user that he should close unused files.



                                                                                                  Your idea of dynamically extending virtual memory doesn't solve the issue, for the machine is limited on resources, so you should be carefull & handle memory issues (or at least, be carefull with them).



                                                                                                  A couple of hints i've seen with memory leaks is:



                                                                                                  --> Keep on mind that if you put something into a collection and afterwards forget about it, you still have a strong reference to it, so nullify the collection, clean it or do something with it... if not you will find a memory leak difficult to find.



                                                                                                  --> Maybe, using collections with weak references (weakhashmap...) can help with memory issues, but you must be carefull with it, for you might find that the object you look for has been collected.



                                                                                                  --> Another idea i've found is to develope a persistent collection that stored on database objects least used and transparently loaded. This would probably be the best approach...






                                                                                                  share|improve this answer















                                                                                                  If you keep on allocating & keeping references to object, you will fill up any amount of memory you have.



                                                                                                  One option is to do a transparent file close & open when they switch tabs (you only keep a pointer to the file, and when the user switches tab, you close & clean all the objects... it'll make the file change slower... but...), and maybe keep only 3 or 4 files on memory.



                                                                                                  Other thing you should do is, when the user opens a file, load it, and intercept any OutOfMemoryError, then (as it is not possible to open the file) close that file, clean its objects and warn the user that he should close unused files.



                                                                                                  Your idea of dynamically extending virtual memory doesn't solve the issue, for the machine is limited on resources, so you should be carefull & handle memory issues (or at least, be carefull with them).



                                                                                                  A couple of hints i've seen with memory leaks is:



                                                                                                  --> Keep on mind that if you put something into a collection and afterwards forget about it, you still have a strong reference to it, so nullify the collection, clean it or do something with it... if not you will find a memory leak difficult to find.



                                                                                                  --> Maybe, using collections with weak references (weakhashmap...) can help with memory issues, but you must be carefull with it, for you might find that the object you look for has been collected.



                                                                                                  --> Another idea i've found is to develope a persistent collection that stored on database objects least used and transparently loaded. This would probably be the best approach...







                                                                                                  share|improve this answer














                                                                                                  share|improve this answer



                                                                                                  share|improve this answer








                                                                                                  edited Mar 23 at 8:52









                                                                                                  Hearen

                                                                                                  3,03211630




                                                                                                  3,03211630










                                                                                                  answered Sep 29 '10 at 7:47









                                                                                                  SoulWandererSoulWanderer

                                                                                                  259210




                                                                                                  259210















                                                                                                      protected by Community Oct 26 '11 at 14:57



                                                                                                      Thank you for your interest in this question.
                                                                                                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                      Would you like to answer one of these unanswered questions instead?



                                                                                                      Popular posts from this blog

                                                                                                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                                                                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                                                                                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript