Java memory clean up

Java memory clean up

You can also generalize it to cleanup thread local variables for other threads: Solution 3: There is no way to cleanup values except from within the thread that put them in there in the first place (or when the thread is garbage collected — not the case with worker threads). Thread local memory leaks should not normally be a major issue with bounded thread pools since any thread locals are likely to get overwritten eventually; i.e. when the thread is reused.

Java memory clean up

We have created the custom test-sutomation framework for our project. This mimics the interaction (mainly json two way data flow in GET and POST request) between browser and server using HTTP client.

Here is hot it happens, We have some 1000 test cases automated. Test case execution involves creation of tonnes of messages and hence tonnes of strings; reading large files repeatedly and verifying the contents and off-course throwing everything after getting verification result. It goes for all the test cases, takes anywhere between 15 to 20 hours.

The problem is that, we are stuck with OutOfMemory Error. Any Idea?; on cleaning up the memory which is consumed after reading and parsing from large files 100MB — 250MB. There are lot of splits, replace, substring operations going on.

Please, Can anybody provide tips,suggestions?

This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space. The OutOfMemoryError objects are created by the JVM when suppression is disabled and/ir the stack trace is not writable.

Читайте также:  Html page templates layout

Solution for OutOfMemoryError: 1. The most obvious solution to this error is to increase the available memory size for the Java Virtual Machine. If your application requires more memory then, you shall grant it to your application.

2. Verify that your application does not store unnecessary information. Store and maintain only those pieces of information required for the proper execution of your Java application.

3. You can use the availabe memory analyzer tools, in order to carefully observe the portions of memory occupied by your application. Examples of such tools are the Eclipse Memory Analyzer (http://www.eclipse.org/mat/) and Java Heap Analysis Tool (jhat)(http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html).

4. Try to configure your JVM to use more memory as shown before (-Xms750m -Xmx2048m -****:MaxPermSize=1024m ).

5. Enable garbage collection logging (-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log) and see how it behaves, how heap is growing. Probably you have a memory leak .

6. If so, take a HeapDump, use YourKit to open it and look for objects that use the largest amount of memory. Try to figure out why and fix it.

7. You can call Garbage collector using:

When calling the Garbage collector method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won’t prevent it. Better investigate why you’re leaking so much memory and clean it up along the way. But this does not mean that it’ll be executed immediately.

Read more about JVM Garbage Collection Tuning SE 6 HotSpot for details on how to tune garbage collection with Java SE 6.

In general, you should make sure you maximize the objects available for garbage collection. One way to do this is declaring variables in the narrowest possible scope. (Eg: inside a try block, use method local variable s)

You may also have to increase Java heap size.

You can explicitly make the objects to null.

Even you can call System.gc ();

What is the best way to clean up an Object in Java?, So 1) use finally blocks to release non-object resources 2) let the garbage collector clean up object-resources 3) you can hint to the garbage collector that you are done with an object by setting it to null if it is used in a long-running method. Share. answered Sep 4, 2009 at 17:56. SingleShot. Code sampleRuntime.getRuntime().addShutdownHook(new Thread() >);Feedback

Java cleanup step

In java I remember there was some method you could override to be called when the jvm exits or a class is destroyed, almost like some cleanup step? I cant seem to find what it was called anywhere.Anybody know what it is called, I just cant find it?

You can add a shutdown hook that will be called when the JVM terminates via Runtime.addShutdownHook() .

Runtime.getRuntime().addShutdownHook(new Thread() < public void run() < // . >>); 

Shutdown hooks are not guaranteed to run if the JVM exits abnormally though.

As @Kaleb points out, you can overload Object.finalize() which will be called when an object is eligible for garbage collection. As Josh Bloch points out in Effective Java Item 7:

Finalizers are unpredictable, often dangerous and generally unnecessary

followed a little lower by (emphasis by Josh):

It can take arbitrarily long between the time that an object becomes unreachable and the time that its finalizer is executed . never do anything time-critical in a finalizer.

If you need to clean up resources in a class, do it in a finally block or implement a close method (or similar) instead of relying on finalize() .

The finalize() method is called when an object gets destroyed.

Kubernetes Job Cleanup, Sorted by: 63. It looks like starting with Kubernetes 1.6 (and the v2alpha1 api version), if you’re using cronjobs to create the jobs (that, in turn, …

How to clean up ThreadLocals

Does any one have an example how to do this? Are they handled by the garbage collector? I’m using Tomcat 6.

«Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

If your application or (if you are talking about request threads) container uses a thread pool that means that threads don’t die. If necessary, you would need to deal with the thread locals yourself. The only clean way to do this is to call the ThreadLocal.remove() method.

There are two reasons you might want to clean up thread locals for threads in a thread pool:

  • to prevent memory (or hypothetically resource) leaks, or
  • to prevent accidental leakage of information from one request to another via thread locals.

Thread local memory leaks should not normally be a major issue with bounded thread pools since any thread locals are likely to get overwritten eventually; i.e. when the thread is reused. However, if you make the mistake of creating a new ThreadLocal instances over and over again (instead of using a static variable to hold a singleton instance), the thread local values won’t get overwritten, and will accumulate in each thread’s threadlocals map. This could result in a serious leak.

Assuming that you are talking about thread locals that are created / used during a webapp’s processing of an HTTP request, then one way to avoid the thread local leaks is to register a ServletRequestListener with your webapp’s ServletContext and implement the listener’s requestDestroyed method to cleanup the thread locals for the current thread.

Note that in this context you also need to consider the possibility of information leaking from one request to another.

Here is some code to clean all thread local variables from the current thread when you do not have a reference to the actual Thread local variable . You can also generalize it to cleanup thread local variables for other threads:

 private void cleanThreadLocals() < try < // Get a reference to the thread locals table of the current thread Thread thread = Thread.currentThread(); Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Object threadLocalTable = threadLocalsField.get(thread); // Get a reference to the array holding the thread local variables inside the // ThreadLocalMap of the current thread Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true); Object table = tableField.get(threadLocalTable); // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object // is a reference to the actual ThreadLocal variable Field referentField = Reference.class.getDeclaredField("referent"); referentField.setAccessible(true); for (int i=0; i < Array.getLength(table); i++) < // Each entry in the table array of ThreadLocalMap is an Entry object // representing the thread local reference and its value Object entry = Array.get(table, i); if (entry != null) < // Get a reference to the thread local object and remove it from the table ThreadLocal threadLocal = (ThreadLocal)referentField.get(entry); threadLocal.remove(); >> > catch(Exception e) < // We will tolerate an exception here and just log it throw new IllegalStateException(e); >> 

There is no way to cleanup ThreadLocal values except from within the thread that put them in there in the first place (or when the thread is garbage collected — not the case with worker threads). This means you should take care to clean up your ThreadLocal’s when a servlet request is finished (or before transferring AsyncContext to another thread in Servlet 3), because after that point you may never get a chance to enter that specific worker thread, and hence, will leak memory in situations when your web app is undeployed while the server is not restarted.

A good place to do such cleanup is servletrequestlistener .requestDestroyed().

If you use Spring, all the necessary wiring is already in place, you can simply put stuff in your request scope without worrying about cleaning them up (that happens automatically):

RequestContextHolder.getRequestAttributes().setAttribute("myAttr", myAttr, RequestAttributes.SCOPE_REQUEST); . . . RequestContextHolder.getRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST); 

Reading again the Javadoc documentation carefully:

‘Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist). ‘

There is no need to clean anything, there is an ‘AND’ condition for the leak to survive. So even in a web container where thread survive to the application, as long as the webapp class is unloaded ( only beeing reference in a static class loaded in the parent class loader would prevent this and this has nothing to do with ThreadLocal but general issues with shared jars with static data ) then the second leg of the AND condition is not met anymore so the thread local copy is eligible for garbage collection.

Thread local can’t be the cause of memory leaks, as far the implementation meets the documentation.

Java — Cleanup after all junit tests, kill -9 will stop the java process without running the shutdown hook. But a shutdown hook is still much preferred to testRunFinished() when there’s a …

Источник

Java memory clean up

This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space. The OutOfMemoryError objects are created by the JVM when suppression is disabled and/ir the stack trace is not writable.

Solution for OutOfMemoryError: 1. The most obvious solution to this error is to increase the available memory size for the Java Virtual Machine. If your application requires more memory then, you shall grant it to your application.

2. Verify that your application does not store unnecessary information. Store and maintain only those pieces of information required for the proper execution of your Java application.

3. You can use the availabe memory analyzer tools, in order to carefully observe the portions of memory occupied by your application. Examples of such tools are the Eclipse Memory Analyzer(http://www.eclipse.org/mat/) and Java Heap Analysis Tool (jhat)(http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html).

4. Try to configure your JVM to use more memory as shown before (-Xms750m -Xmx2048m -XX:MaxPermSize=1024m ).

5. Enable Garbage Collection logging (-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log) and see how it behaves, how heap is growing. Probably you have a memory leak.

6. If so, take a HeapDump, use YourKit to open it and look for objects that use the largest amount of memory. Try to figure out why and fix it.

7. You can call Garbage collector using:

When calling the Garbage collector method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won’t prevent it. Better investigate why you’re leaking so much memory and clean it up along the way. But this does not mean that it’ll be executed immediately.

Read more about JVM Garbage Collection Tuning SE 6 HotSpot for details on how to tune garbage collection with Java SE 6.

Solution 2

In general, you should make sure you maximize the objects available for garbage collection. One way to do this is declaring variables in the narrowest possible scope. (Eg: inside a try block, use method local variables)

Источник

Оцените статью