Tuesday, July 20, 2010

Different out of memory error reasons

Exception in thread "main": java.lang.OutOfMemoryError: Java heap space

The detail message Java heap space indicates that an object could not be allocated in the Java heap. This error does not necessarily imply a memory leak. The problem can be as simple as a configuration issue, where the specified heap size (or the default size, if not specified) is insufficient for the application.

Exception in thread "main": java.lang.OutOfMemoryError: PermGen space

The detail message PermGen space indicates that the permanent generation is full. The permanent generation is the area of the heap where class and method objects are stored. If an application loads a very large number of classes, then the size of the permanent generation might need to be increased using the -XX:MaxPermSize option.

Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit

Indicates that the application (or APIs used by that application) attempted to allocate an array that is larger than the heap size

Exception in thread "main": java.lang.OutOfMemoryError: request bytes for . Out of swap space?

The HotSpot VM code reports this apparent exception when an allocation from the native heap failed and the native heap might be close to exhaustion. The message indicates the size (in bytes) of the request that failed and the reason for the memory request. In most cases the part of the message is the name of a source module reporting the allocation failure, although in some cases it indicates a reason aswell. When this error message is thrown, the VM invokes the fatal error handling mechanism, that is, it generates a fatal error log file, which contains useful information about the thread, process, and system at the time of the crash. In the case of native heap exhaustion, the heap memory and memory map information in the log can be useful

The problem might not be related to the application, for example:

· The operating system is configured with insufficient swap space

· Another process on the system is consuming all memory resources

Or

· It is possible that the application failed due to a native leak, for example, if application or library code is continuously allocating memory but is not releasing it to the operating system

Exception in thread "main": java.lang.OutOfMemoryError: (Native method)

If the detail part of the error message is (Native method) and a stack trace is printed in which the top frame is a native method, then this is an indication that a native method has encountered an allocation failure. The difference between this and the previous message is that the allocation failure was detected in a JNI or native method rather than in Java VM code

Some causes of memory leaks in JAVA

· Collection classes, such as hashtables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application

· Failure to remove Stale Object References from Data Structures

· A reference to a short-term object is used transiently by a long-term object but the long-term object does not clear the reference when it’s done with it

· When the user register a class as an event listener without bothering to unregister when the class is no longer needed

o If you register an object, do also unregister

o If you add Observers to Observables, think also about remove them

o If you install actions to GUI components, do also deinstall.

· Member variables of a class that point to other classes not to be set to null at the appropriate time

· Do never build caches without limits

· Do never build pools without limits

· There are cases (ThreadLocal, for instance) where other objects indirectly reference the class loader causing it to remain uncollected

· If the finalizer method throws an error or exception, a memory leak will occur

· Via not properly closing the objects that needs to be closed like ResultSet,Stream etc

· If you use WeakHashMap, the key object must not be strongly reachable by the value object

RAD Performance Tips

Preference

  • When Build automatically (auto build) is turned on, it means that every time you save a file, an incremental build happens. This includes not only building (or compiling) the file, but also building the files that depends on it. It also runs validation. Turning auto build off will improve the performance of your save operations at the expense of slower builds and less up-to-date error information
  • Turning Refresh workspace automatically on will start a background process that searches for changes that have been made by other processes to resources in the workspace. If changes are found, this will cause an incremental build to run if auto build is turned on, but otherwise it does not initiate any builds
  • By turning off some of the capabilities, you will remove certain user interface items. This in turn will prevent you from invoking certain functions. Since Rational Application Developer uses lazy loading, any time you invoke a new function, it takes more memory to hold the classes that implement the new function
  • Any editor that is active (that is, has focus) when Rational Application Developer is shut down, will become active again when Rational Application Developer is restarted. To improve the startup time of Rational Application Developer, you can select the Close all editors on exit
  • Quick diff is a function that shows a line in the margin to show you what has changed. Disabling quick diff will slightly improve your performance while editing files
  • Disabling all or some of the label decorations will slightly improve your performance
  • The Links Builder keeps track of hyperlinks. If you have a lot of HTML files in your workspace, there can be a lot of links to keep track of. In some cases, disabling the Links Builder can greatly improve your build times
  • Running all of the validators all of the time is a reasonably expensive operation. If you are finding that your builds are taking too long, you could turn off all or some of the validators
  • Monitor, how much memory is being used by turning on the Show heap status preference in Rational Application Developer in case of performance degradation

Other

  • Keep the projects that you intend on changing in source form, and the projects that you are just using in binary form
  • As a general rule you should not have projects in your workspace that are not needed. They just cause extra overhead when building, validating and indexing your workspace
  • Rational Application Developer is a very large product; a typical installation has over 100, 000 files. The defragmentation of the hard disk after installing Rational Application Developer can improve startup time
  • Install only what you will use in your application development and uninstall features that you no longer require
  • Periodically starting with a clean workspace, and re-importing your source will improve performance. This is quite easy to do if your projects are all stored in a source control system
  • Over time, there is metadata that accumulates in the workspace (in the .metadata directory). This includes things like log files, for instance. Cleaning this up every so often means that there are less things needed to keep track of for Rational Application Developer
  • Running test server on another remote machine will free up resources
  • As you use more functions, more classes are loaded into memory. Rational Application Developer uses lazy loading, so the code for a particular function is not loaded until it is required. For example, if you do not usually use the XML editor, but in this session you edited one XML file, the XML editor is loaded only when it is needed. However, once it is loaded it will not be un-loaded. The only way to free this memory is to close and restart Rational Application Developer. You should close Rational Application Developer at least once a day
  • Never minimize Rational Application Developer since that will initiate memory swap
  • Change the -Xmaxf –Xminf parameters
  • Set the heap size as

size of physical memory – memory used for OS – 100MB(Other uses)

This will be almost equivalent to 2/3 of your available memory.

  • When you are working with WebSphere Test Environment (WTE) inside RAD, there will be a heap size associated with WTE as well. So put ¼ the of the total makeable max heap size for WET and the rest for RAD. To specify the WTE heap within Application Developer, go to the Server perspective Server Configuration view, and open your server instance Editor Window. Select the Environment tab and enter –Xmx..M in the Java VM Arguments window. Then save and close that server editor window. A similar change is required if you run a stand-alone WAS outside of Application Developer (instead of using the embedded WTE)
  • Rational Application Developer uses shared classes to improve startup time. This is enabled by specifying the -Xshareclasses directive in the eclipse.ini file. If you need to save memory, you can remove this directive as well as the -Xscmx96m directive; however, removing these directives will increase your startup times
  • It is considerably faster to restart a project instead of restarting the server

Test Server Configuration

  • The Enable hot method replaces in debug mode option should always be enabled. Not only does this option allow hot method replace, but more significantly from a performance point of view, it enables "Just In Time" compilation while in debug mode. This greatly speeds up how quickly your application runs and how fast the server starts. This option only applies to debug mode, and has no effect when running the server in normal mode
  • If you don't need the universal test client (it is enabled by default), you should clear its checkbox so that it wont be installed
  • The Optimize server for testing and developing option should always be checked. This sets some JVM parameters that improves the startup time of the server
  • Set the publishing option to never publish automatically

Test Server Admin Console Configuration

  • Once the Admin Console is running, select the server (it is usually called server1; Servers->Application Servers->Server1), and ensure that the Run in development mode and Parallel start options are selected. This will provide even more server startup improvements
  • Check if there are any unneeded applications that could be removed. There are a couple of small sample applications (DefaultApplication, ivtApp and query) that are installed by default. These can be safely removed
  • If you know that your module does not contain any Java EE annotations, set the Metadata Complete property to true in the web.xml deployment descriptor. This will signal to WebSphere Application Server that it does not need to scan the classes and JAR files for annotations