Question

Demonstrate the following in java: Proper handling of memory and objects to avoid memory leaks

Demonstrate the following in java:

Proper handling of memory and objects to avoid memory leaks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.First of all what is memory leak

A Memory Leak is a situation when there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory and, thus they are unnecessarily maintained. A memory leak is bad because it blocks memory resources and degrades system performance over time.

A memory leak is bad because it blocks memory resources and degrades system performance over time. And if not dealt with, the application will eventually exhaust its resources, finally terminating with a fatal java.lang.OutOfMemoryError.

2. Why does it occurs

Java does automatic Garbage collection. However there can be situations where garbage collector does not collect objects because there are references to them. There might be situations where an application creates lots of objects and does not use them. Just because every objects has valid references, garbage collector in Java can’t destroys the objects. Such types of useless objects are called as Memory leaks. If allocated memory goes beyond limit, program will be terminated by rising OutOfMemoryError.

3 .Now the question arises how to handle object to avoid memory leak

* To get rid of memory leak you can  reference objects

EXPLANATION OF ABOVE POINT:

Using the java.lang.ref package, you can work with the garbage collector in your program. This allows you to avoid directly referencing objects, but use special reference objects that are easily cleared by the garbage collector. The special subclasses allow you to refer to objects indirectly. For instance, Reference has three subclasses: PhantomReference, SoftReference, and WeakReference.

A referent, or an object that is referenced by these subclasses, can be accessed using that reference object’s get method.How does garbage collector act with each type of referent?

  • SoftReference object: Garbage collector is required to clear all SoftReference objects when memory runs low.
  • WeakReference object: When garbage collector senses a weakly referenced object, all references to it are cleared and ultimately taken out of memory.
  • PhantomReference object: Garbage collector would not be able to automatically clean up PhantomReference objects, you would need to clean it up manually by clearing all references to it.

ADVANTAGES

  • The advantage of using this method is that you can clear a reference easily by setting it to null and that the reference is pretty much immutable, or it cannot be changed.
  • With the use of reference objects, you can work with the garbage collector to automate the task of removing listeners that are weakly reachable. WeakReference objects, especially with a cleanup thread, can help you avoid memory errors.

4.Other ways to prevent memory leak

  • Release the session when it is no longer needed. Use the HttpSession.invalidate() to do this.
  • Keep the time-out time low for each session.
  • Store only the necessary data in your HttpSession.
  • Avoid using string concatenation. Use StringBuffer’s append() method because the string is an unchangeable object while string concatenation creates a lot of unnecessary objects. A large number of temporary objects will slow down performance.
  • As much as possible, you should not create HttpSession in your jsp page. You can do this by using the page directive <%@page session=”false”%>.
  • If you are writing a query that is frequently executed, use PreparedStatement object rather than using Statement object. Why? PreparedStatement is precompiled while Statement is compiled every time your SQL statement is transmitted to the database.
  • When using JDBC code, avoid using “*” when you write your query. Try to use the corresponding column name instead.
  • If you are going to use stmt = con.prepareStatement(sql query) within a loop, then be sure to close it inside that particular loop.
  • Be sure to close the Statement and ResultSet when you need to reuse these.
  • Close the ResultSet, Connection, PreparedStatement, and Statement in the finally block.

Thanks hope it will help you!

if does then please rate

Add a comment
Know the answer?
Add Answer to:
Demonstrate the following in java: Proper handling of memory and objects to avoid memory leaks
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT