Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

What You Can Do To Deal With Java’s Memory Retention Problems

By Exforsys | on July 22, 2006 |
J2EE

What You Can Do To Deal With Java’s Memory Retention Problems

When you work with the Java programming language, a feature that you will want to be familiar with is called finalization.

Finalization will allow you to conduct a cleanup on objects that the garbage collector is not capable of reaching. Finalization will generally be used to recapture resources which are connected to an object. Below is an example of a basic finalization:

public class Image1 {
// pointer to the native image data
private int nativeImg;
private Point pos;
private Dimension dim;

// it disposes of the native image;
// successive calls to it will be ignored
private native void disposeNative();
public void dispose() { disposeNative(); }
protected void finalize() { dispose(); }

static private Image1 randomImg;
}

Once the Imagel becomes impossible to reach, the JVM will request a finalize technique to make sure the resource that holds the image information will be recaptured. The finalize() technique is a method which holds arbitrary information. It can access any part of the object, and in the above example it has accessed dim and pos. In addition to this, it can also allow the object to become reachable. While this technique is not recommended by some programmers, the Java programming language allows it. I will now list the steps to show the lifespan of an object which is finalized. A finalized object is one that has a class which is non-trivial.

When the object is first registered, the Java Virtual Machine will record that this object is finalized. This may slow down the fast register path that the newer JVMs contain. After this occurs, the garbage collector will decide if the object is able to be reached. It will notice that the object has been finalized, and it will then add the object to the finalization queue. It will also make sure that all the objects which can be reached are retained, and this will allow the finalizer to gain access to them. Once this has occured, the Java Virtual Machine will use what is called a finalizer thread. The finalizer thread will dequeue the object, record the finalizer of the object, and bring forth the finalize method. Once these three things have been done, the object will be finalized.

Once the object has been finalized, it will be studied by the garbage collector. When the garbage collector sees that the object cannot be reached, it will recapture the space for the object in addition to everything which can reached within this space. The garbage collector will need two cycles to recapture an object, and it will need to recapture other objects which are reachable from the object during this procedure. If the programmer is not cautious, they could create a temporary data retention problem which will be unstable. There is no guarantee that the Java Virtual Machine will request finalizers of all the objects which have been finalized. It may cancel before the garbage collector finds that some of the objects can’t be reached.

Even if you use finalization correctly, you may find that the recapture process is extended. It is possible to correct this problem by arranging the code so that it uses a "contains" instead of "extends" pattern. An example of this code can be seen here:

public class RGBImage2 {
private Image1 img;
private byte rgbData[];

public void dispose() {
img.dispose();
}
}

This example contains an appearance of Image1 instead of just extending it. If an appearance of RGBImage2 can’t be reached, the garbage collector will quickly recapture it, and it will also capture the rgbData array. After this, it will queue up the appearance of Image1 to be finalized. It is not always possible to arrange your code in the method used above. You may be required to do a bit more work to make sure the instances do not take up more than they are supposed to when they are finalized. In the example below, I will show you how this can be done:

public class RGBImage3 extends Image1 {
private byte rgbData[];

public void dispose() {
super.dispose();
rgbData = null;
}
}

As you can see, RGBImage3 is the same as RGBImage1, but it uses the dispose( ) technique, which cancels the rgbData field. The techniques discussed in this article can effectively allow you to deal with the memory retention problems that are inherent in Java. Memory plays an important role in virtually any application, and it is important for you to learn how to utilize it properly.

« « How To Set An Example For Your Followers
How To Bring Out The Best In Those You Lead » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • What You Should Know About Java XML

    July 25, 2006 - 0 Comment
  • New Features In Java 5.0 – From Managers Point of View

    November 26, 2005 - 0 Comment
  • How To Create High Quality Tables With JavaScript

    July 5, 2006 - 0 Comment
  • Why It is Important To Focus On Java Exceptions For Your Programs

    July 27, 2006 - 0 Comment
  • New Features in Java 5 – From Programmers Point of View

    November 26, 2005 - 0 Comment
  • Determine the Effectiveness of your Java Software

    July 5, 2006 - 0 Comment
  • Important Features of Java – Multithreading, AWT

    July 26, 2006 - 0 Comment
  • Performance Tuning of Java Applications

    December 2, 2005 - 0 Comment
  • How To Develop RFID Applications In Java

    July 8, 2006 - 0 Comment
  • J2EE Overview

    December 6, 2005 - 0 Comment
  • Why It is Important To Focus On Java Exceptions For Your Programs

    July 27, 2006 - 0 Comment
  • Important Features of Java – Multithreading, AWT

    July 26, 2006 - 0 Comment
  • What You Should Know About Java XML

    July 25, 2006 - 0 Comment
  • Java Overview

    July 17, 2006 - 0 Comment
  • How To Use Java DB as Your Client Mobile Database

    July 14, 2006 - 0 Comment
  • How To Run J2ME Programs on Palm Devices

    July 14, 2006 - 0 Comment
  • Java Virtual Machine

    July 13, 2006 - 0 Comment
  • Quickly Develop Java Programs With Tapestry

    July 12, 2006 - 0 Comment
  • A Java TOC2 Class Which Can Contact Aim

    July 12, 2006 - 0 Comment
  • How To Perform Class Loading With Java

    July 8, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Why It is Important To Focus On Java Exceptions For Your Programs
  • Important Features of Java – Multithreading, AWT
  • What You Should Know About Java XML
  • Java Overview
  • How To Use Java DB as Your Client Mobile Database

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT