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

后端 未结 21 1682
死守一世寂寞
死守一世寂寞 2020-11-21 04:49

I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryEr

相关标签:
21条回答
  • 2020-11-21 05:22

    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.

    0 讨论(0)
  • 2020-11-21 05:22

    In android studio add/change this line at the end of gradle.properties (Global Properties):

    ...
    org.gradle.jvmargs=-XX\:MaxHeapSize\=1024m -Xmx1024m 
    

    if it doesn't work you can retry with bigger than 1024 heap size.

    0 讨论(0)
  • 2020-11-21 05:23

    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...

    0 讨论(0)
  • 2020-11-21 05:24

    If everything else fails, in addition to increasing the max heap size try also increasing the swap size. For Linux, as of now, relevant instructions can be found in https://linuxize.com/post/create-a-linux-swap-file/.

    This can help if you're e.g. compiling something big in an embedded platform.

    0 讨论(0)
  • 2020-11-21 05:25

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

    See here for details.

    0 讨论(0)
  • 2020-11-21 05:25

    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.

    0 讨论(0)
提交回复
热议问题