Question

We have a Java application which is kinda huge and requires too much memory, although I set the initial and maximum memory using Xm flags, but sometimes it freezes, and we get an OutOfMemoryError after 1-2 min (if we're lucky).

We need to restart the application if this happens.

We run the program by a native c application which simply runs a command line.

Questions

  1. What if I don't set the Xmx flag?
  2. Does it mean unlimited memory usage?
  3. How can I realize if the application is frozen in either C or Java?
Était-ce utile?

La solution

What if I don't set the Xmx flag?

Then the JVM defaults to a (typically modest) size of memory for the app.

Does it mean unlimited memory usage?

No.

How can I realize if the application is frozen in either C or Java?

Fix the memory leak1.

Note that the question I answered used 'release' as opposed to 'realize'. After careful consideration, I feel the answer should remain exactly the same. Either 'releasing (memory)' or 'realizing (there is a problem)' is not going to actually fix the problem. It is better either way to fix the actual problem (a memory leak).


As an aside. I don't think the problem is in the C code, but a simple test (launch the app. from the command line) should reveal either way. The reason I think the problem is the Java code is that the OutOfMemoryError is detected by a (presumably) fully functional JVM.

If the C app. had used memory space that the JVM reserved for itself or the app. I guess the error would be very different, possibly along the lines of: JVM internal error..

1. Fixing the memory leak

There has been a range of good advice offered by other contributors, that I will revisit here.

  • @ChristianKullmann started it off by stressing the importance of memory profiling.
  • @KlasLindbäck followed it up with some excellent resources linked in a comment.
    • Java Heap Dump.
      "One way to find memory leaks is analyzing heap dumps. There are several ways to get a heap dump (not including 3rd party tools).."
    • Tips and tricks for analyzing Java virtual machine heap memory dumps.
      "Memory dumps are a very useful feature of the JVM to analyze the contents of the memory at any given time, but their usage requires some experience, and in this post I will share with you some tips and tricks that I’ve learned over the years, so hopefully they will be useful to you too."
  • @mKorbel rounded it out with some typical problems to look for:
    *"search in code if there are close() all:
    • JDBC,
    • FileIO,
    • Socket,
    • etc and in finally block, then you'd love re_engeneering too much"*

Autres conseils

Running your application using C native code does not mean that you may be able to steer the whole memory usage by the JVM parameters. Try running a memory monitoring for your system (e.g. TOP on Linux with shift+m for memory usage sorting) or better yet profile your application for memory.

Try to get a memory dump after running your application (there are some profiling tools out there, most IDEs provide some feature as well) to check your java objects.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top