JAVA Heap stack Error - java

I have developed a barcode billing and inventory software using netbeans 6.5. I dont know why when the application runs for some time then some times it gives a JAVA Heap stack (Out of Memory) err. I know there is some way to handle the memory allocation in netbeans. Could anyone please help me on this..
Thankx

You need to figure out if your application needs a bigger working memory than the default setting, or if it is just leaking. If there is a memory leak (which is a common problem), then increasing the total memory will only give your application more time before it crashes. It's easy to do (as other posters have suggested) and it will show you if there is a bad leak, so try it first. If it keeps growing in memory, you need to look at what your application is holding onto in memory. Have a look at JConsole (which comes with Java6) or JHat or other tools.

You should pass the argument -Xmx1024m when starting your program, so that the jvm can use more heap for your application. This -Xmx will give your programm 1024mb of ram.

Use java -Xmx<size>m to set the maximum size for the heap. And use a memory profiler like JMP(Java Memory Profiler) to figure out the memory consumption.
Similar questions on SO:
java-lang-outofmemoryerror-java-heap-space-with-netbeans
java-heap-space-in-netbeans-but-ive-increased-the-heap-size-already

Related

How resolve an OutOfMemory Error by the better way

I am programming a Java application allowing to minimize any boolean expression using QuineMcCluskey methodology. When I compile my code, I have an OutOfMemory Error with the message "Java heap space"!
If I understand, the exception may have several origins:
The memory space allocated to the JVM heap is insufficient to create the objects required by the application.
A memory leak prevents the garbage collector from releasing objects that are yet unused but still have references. Thus these objects are never released and occupy more and more space in the pile until occupying all the available space.
...
I know that use a profiling tool may be necessary to analyze the contents of the memory of the JVM and determine the origin of the memory consumption. But how use thoses tools ? Have I to modify xmx and xms data ? What could be the consequences if I change them ?
(I know also that it is necessary to optimize my code).
What are the different debugging steps ?
Furthermore, this application has to be use by lot of users (so by diferent computers...)
As you can see, I have lot of questions about this problem (I am a novice lol)... That's why I create this post.. I would like to resolve this problem by the better way and also, learn good reflexes.
Thank you!
Increasing of the memory is NOT the solution, unless you have unlimited resources!
If that happens in production or any machine other than your own, you can force the JVM which runs your service to generate a dump file which you can then download and analyse by adding VM flag -XX:+HeapDumpOnOutOfMemoryError to your jvm.conf file.
That is pretty useful when the machine that runs the app becomes unresponsive so no JMX connections could be made in order to run Oracle's JMC (or similar) monitoring tools. If you are for some reason able to get to the VM you can run the flight recording and try to analyse which method is causing the OOM.
Check here how to use flight recording.
And check this for heap dump analysis preview
It is possible to increase heap size allocated by the JVM by using command line options Here we have 3 options
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
java -Xms16m -Xmx64m ClassName
It is also possible to increase heap size allocated by the JVM in eclipse directly In eclipse IDE goto
Run---->Run Configurations---->Arguments
And set VM arguments

My App is taking 250mb memory on my device. When I do a heap dump and analyze it. it says the heap size around 7mb

Is there another memory which is being used that doesn't show on a heap dump? I am using Memory Analyzer on Eclipse. First I dump the heap with DDMS and Memory Analyzer picks. it only shows that the size of the heap is 7mb and the histogram is basically telling me everything is fine. However, I have downloaded several memory monitoring apps on my device and they all say my app is taking 250mb and it keeps climbing.
How else can I try to find out why its taking so much memory?
Memory measuring apps are not worth very much, as, to quote Dianne Hackborn:
Note that memory usage on modern operating systems like Linux is an extremely complicated and difficult to understand area. In fact the chances of you actually correctly interpreting whatever numbers you get is extremely low.
That being said, you are welcome to use the techniques outlined in her answer to try to get a better handle on what those tools are claiming and whether it is really a problem.
Also:
If you have more than one process, bear in mind that DDMS' heap dump is for a single process
If you are doing native development using the NDK, NDK allocations are not part of the managed heap and therefore are not included in the heap dump

Java/Tomcat heap size question

I am not a Java dev, but an app landed on my desk. It's a web-service server-side app that runs in a Tomcat container. The users hit it up from a client application.
The users constantly complain about how slow it is and the app has to be restarted about twice a week, cause things get really bad.
The previous developer told me that the app simply runs out of memory (as it loads more data over time) and eventually spends all its time doing garbage collection. Meanwhile, the Heap Size for Tomcat is set at 6GB. The box itself has 32GB of RAM.
Is there any harm in increasing the Heap Size to 16GB?
Seems like an easy way to fix the issue, but I am no Java expert.
You should identify the leak and fix it, not add more heap space. Thats just a stop gap.
You should configure tomcat to dump the heap on error, then analyze the heap in one of any number of tools after a crash. You can compute the retained sizes of all the clases, which should give you a very clear picture of what is wrong.
Im my profile I have a link to a blog post about this, since I had to do it recently.
No, there is no harm in increasing the Heap Size to 16GB.
The previous developer told me that the app simply runs out of memory (as it loads more data over time)
This looks like a memory leak, a serious bug in application. If you increase the amount of memory available from 6 to 16 GiB, you're still gonna have to restart the application, only less frequent. Some experienced developer should take a look at the application heap while running (look at hvgotcodes tips) and fix the application.
To resolve these issues you need to do performance testing. This includes both CPU and memory analysis. The JDK (6) bundles a tool called VisualVM, on my Mac OS X machine this is on the path by default as "jvisualvm". That's free and bundled, so it's a place to start.
Next up is the NetBeans Profiler (netbeans.org). That does more memory and CPU analysis. It's free as well, but a bit more complicated.
If you can spend the money, I highly recommend YourKit (http://www.yourkit.com/). It's not terribly expensive but it has a lot of built-in diagnostics that make it easier to figure out what's going on.
The one thing you can't do is assume that just adding more memory will fix the problem. If it's a leak, adding more memory may just make it run really badly a bit longer between restarts.
I suggest you use a profiling tool like JProfiler, VisualVM, jConsole, YourKit etc. You can take a heap dump of your application and analyze which objects are eating up memory.

Memory Leak in a Java based application

There is a memory leak happens in an application when a short lived object holds a long lived object,
My question is how can we identify
1) which object lives longer and shorter, any tool which measures life of an object?
2nd Question
I am constantly getting the Out of Memory Space Error and I tried increasing the Heap memory to 2 GB, but still i am getting, please suggest me any open source tool with which i can identify the memory leak issue and fix.
At present I am restarting the server every time as a temporary solution, but Suggest me any thing which i can fix permanently.
You can use the VisualVM tool included in the JDK:
http://download.oracle.com/javase/6/docs/technotes/tools/share/jvisualvm.html
Documentation available here:
https://visualvm.dev.java.net/docindex.html
There are 2 options:
It just may be your application doesn't have enough heap allocated. Measure size of your input and give application corresponding heap;
There's memory-leak: take profiler, examine your heap, find objects which shouldn't be there or there too much of them ('short-living objects', in your terms), identify which 'long-living' object holds them, fix this. You should know your code to understand which objects must be 'short-living' and which must be 'long-living'.
I've found the Heap Walker in Netbeans very usefull
As said, jvisualvm have good tools to analyze the heap live.
But you can also use jvisualvm or -XX:+HeapDumpOnOutOfMemoryError to take a heap dump in a file. And then take the file to your destkop, to open it in Eclipse Memory Analyzer. Eclipse MAT is even better to analyze the memory.
Out of Memory occurs on a server because it literally uses up all memory it's allowed to have. Not sure about what application you're using for hosting the server, but for Apache, you need to add the line -Xmx512m where 512 is the maximum amount of megabytes it's allowed to have.
If you leave the application to run long enough, it's going to happen. This isn't because of memory leaks in Java but the server itself which has a tendency to do so. You can't change this behavior, but you can at least increase the default memory of 256 mb. With the heavy loading site that I work on everyday, 256 mb lasts about 30 minutes for me unfortunately. I've found that 1024 mb is reasonable and rarely crashes due to out of memory exceptions.
I'd strike me as very unusual for Java to be incapable of garbage collecting correctly unless the programmer took a hand at overriding typical functionality.
I think you can track memory leaks with jsconsole (which comes shipped with JDK6 if i'm not mistaken).
A short-lived object holding a reference to a long-lived object will not cause problems. (a good overview , including generational garbage collection).
2GB is an awful lot of objects/references. If you're running out of heap space at 2Gb you're likely holding onto massive amounts of data and/or keeping open resources when you're done with them. You should post at the very least a description of what your application does and how long it takes to die.
You can get some sense of what's happening quickly by watching the garbage collector (e.g. run with "-verbose:gc" which will tell you when the garbage collector is running and how much it collects).

Java memory mystery (do I have a leak)?

I have a standalone Java problem running in a linux server. I started the jvm with -Xmx256m. I attached a JMX monitor and can see that the heap never really passes 256Mb. However, on my linux system when I run the top command I can see that:
1) First of all, the RES memory usage of this process is around 350Mb. Why? I suppose this is because of memory outside of the heap?
2) Secondly, the VIRT memory usage of this process just keeps growing and growing. It never stops! It now shows at 2500Mb! So do I have a leak? But heap doesn't increase, it just cycles!
Ultimately this poses a problem because the swap of the system keeps growing and eventually the system dies.
Any ideas what is going on?
The important question I want to ask, what are some scenarios that this could be a result of my code and not the JVM, kernal, etc. For example, if the number of threads keeps growing, would that fit the description of my observations? Anything similar that you can suggest me to look out for?
A couple of potential problems:
Direct allocated buffers and memory mapped files are allocated outside of the Java heap, and can't conveniently be disposed.
An area of stack is reserved for each new thread.
Permanent generation (code and interned strings) is outside of the usual stack. It can be a problem is class loaders leak (usually when reloading webapps).
It's possible that the C heap is leaking.
pmap -x should show how your memory has disappeared.
Swap Sun vs IBM JVM to test
RES will include code + non-head data. Also, some things that you think would be stored in the heap aren't, such as the thread stack and "class data". (It's a matter of definition but code and class data are controlled by -XX:MaxPermSize=.)
This one sounds like a memory leak in either the JVM implementation, the linux kernel, or in library JNI code.
If using the Sun JVM, try IBM, or vice versa.
I'm not sure exactly how dlopen works, but code accessing system libraries might be remapping the same thing repeatedly, if that's possible.
Finally, you should use ulimit to make the system fail earlier, so you can repeat tests easily.
WRT #1, it's normal for your RSS to be larger than your heap. This is because system libraries and non-Java code are included in the RSS but not the heap size.
WRT #2, Yes, it sounds like you have a leak of some sort. If the system itself is crashing, you are likely consuming too much of a system resources, like sockets, threads, or files.
Try using lsof to see what files the JVM has open. Run this a few times as your memory increases. If the JVM is crashing, be sure to set the -XX:+HeapDumpOnOutOfMemoryError option.
In my experience, the most common cause of non-heap memory leak in Java is thread leak.
A tool you may find useful is jvmtop, which lets you monitor heap size, thread number and other metrics in real time.
Sounds like you have a leak. Can't you do profiling to see which function is driving the memory up? I am not sure though.
If I had to take a stab in the dark, I would say that the JVM you are using has a memory leak.

Categories

Resources