Month: May 2018

Garbage Collection in Java

What is Garbage Collection in Java?

 

In Java, the unreferenced objects not having any reference to them are referred to as garbage as they are useless.

The process of collecting the unreferenced objects (garbage) to free up the unnecessarily occupied memory for efficient memory usage is called as garbage collection.

In Java, there is a program called as Garbage Collector which has been assigned the responsibility of Garbage Collection.

 

 

This ignorance of destruction of useless objects often fills the memory unnecessarily and as a result of which at certain point of time, sufficient memory does not remain available for the creation of new objects and the performance of entire application goes down due to memory problems like OutOfMemoryError.

To avoid such problems, it is very important to perform garbage collection from time to time.

 

Purpose of Garbage Collector-

 

  • Unlike in old programming languages like C++ where programmer has the responsibility of both creating as well as destroying the objects, in Java, programmers are allowed to create as many objects as they want without any overhead of destroying the useless objects.
  • Garbage Collector takes up the responsibility of cleaning up the memory by destroying the useless objects.
  • Garbage Collector runs from time to time in the background silently to clean up the memory.

 

Eligibility Criteria for Garbage Collection-

 

  • Although, programmers have no responsibility of destroying the useless objects, it is highly recommended that if there are some existing objects which are no longer required, programmers make those objects eligible for the garbage collection so that they can be easily identified by the garbage collector for garbage collection.

 

An object qualifies for garbage collection if and only if it does not contain any external reference to it

 

 

Requesting JVM to run Garbage Collector-

 

  • We can not predict when JVM will perform the garbage collection by running its garbage collector. It entirely depends on the JVM and varies from JVM to JVM.
  • So, an object may not be destroyed immediately after it has qualified for the garbage collection. It will be collected only when the JVM will run its garbage collector.
  • Instead of keep waiting for the JVM to run the garbage collector by itself, we can manually make a request to the JVM for collecting the garbage.
  • However, it totally depends on the JVM whether it accepts the request or not. However, JVM mostly accepts the request for running the garbage collector.

 

Ways to request JVM for running Garbage Collector-

 

There are 2 ways in which the JVM can be requested to run its garbage collector-

 

 

1. By using System class-

 

  • System class contains a static method gc( ) which can be used to request the JVM  to run its garbage collector.
  • Syntax-

System.gc( ) ;

 

2. By using Runtime class-

 

  • Runtime class is a singleton class contained in a java.lang package.
  • It is possible for a java application to communicate with the JVM by making use of a Runtime object which can be created by using getRuntime( ) method as-

Runtime obj = Runtime.getRuntime( ) ;

  • Once we have created a Runtime object, the methods given below can be called on it-

 

a. freeMemory( )-

  • This method returns the amount of free space in the heap in bytes.

b. totalMemory( )-

  • This method returns the total heap size in bytes.

c. gc( )-

  • This method requests the JVM to perform the garbage collection by running its garbage collector.

 

NOTE-

 

  • gc( ) method contained in System class is static in nature i.e. it is a static method whereas gc( ) method contained in Runtime class is an instance method.

 

Which of the above two ways is recommended?

 

As per the convenience, it is recommended to use System.gc( ) method when compared to Runtime class gc( ) method.

As per the performance performance, it is recommended to use Runtime class gc( ) method when compared with System class gc( ) method because Runtime class gc( ) method calls System.gc( ) method internally.

 

class System
{
   public static void gc( )
   {
      Runtime.getRuntime( ).gc( );
   }
}

 

By using Runtime class gc( ) method directly, a method call is saved which contributes towards improving the performance.

 

EXAMPLE-

import java.util.Date; 
class RuntimeExample
{
   public static void main (String[ ] args)
   {
      Runtime obj = Runtime.getRuntime( ); 
      System.out.println(obj.totalMemory( ));
      System.out.println(obj.freeMemory( )); 

      for(int j=0 ; j<10000 ; j++)
      {
         Date d = new Date( ); 
         d = null; 
      }

      System.out.println(obj.freeMemory( ));
      obj.gc( );
      System.out.println(obj.freeMemory( ));
   }
}

OUTPUT-

5177344

4945200

4714464

5059352

 

PROBLEM BASED ON GARBAGE COLLECTION IN JAVA-

 

Problem-

A student learning Java states the following 4 ways in which the JVM can be requested for running its Garbage Collector-

  1. Runtime.getRuntime( ).gc( );
  2. Runtime.gc( );
  3. (new Runtime( )).gc( );
  4. System.gc( );

Which of the above four ways is / are correct?

  1. Only I and IV
  2. Only I
  3. Only IV
  4. All are correct

 

Solution-

Option (A) is correct.

  • II is incorrect because gc( ) method contained in Runtime class is not static in nature.
  • III is incorrect because Runtime class is a singleton class, so an object can not be created for it by using the new operator.

 

Get more notes and other study material of Core Java.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Concurrency Problems | DBMS

Transactions in DBMS-

 

Before you go through this article, make sure that you have gone through the previous article on Transactions in DBMS.

 

We have discussed-

  • A transaction is a set of logically related operations.
  • A transaction goes through different states throughout its life cycle.
  • ACID Properties are followed by each transaction to ensure the consistency of database.

 

In this article, we will discuss about concurrency problems of transactions.

 

Concurrency Problems in DBMS-

 

  • When multiple transactions execute concurrently in an uncontrolled or unrestricted manner, then it might lead to several problems.
  • Such problems are called as concurrency problems.

 

The concurrency problems are-

 

 

  1. Dirty Read Problem
  2. Unrepeatable Read Problem
  3. Lost Update Problem
  4. Phantom Read Problem

 

1. Dirty Read Problem-

 

Reading the data written by an uncommitted transaction is called as dirty read.

 

This read is called as dirty read because-

  • There is always a chance that the uncommitted transaction might roll back later.
  • Thus, uncommitted transaction might make other transactions read a value that does not even exist.
  • This leads to inconsistency of the database.

 

NOTE-

 

  • Dirty read does not lead to inconsistency always.
  • It becomes problematic only when the uncommitted transaction fails and roll backs later due to some reason.

 

Example-

 

 

Here,

  1. T1 reads the value of A.
  2. T1 updates the value of A in the buffer.
  3. T2 reads the value of A from the buffer.
  4. T2 writes the updated the value of A.
  5. T2 commits.
  6. T1 fails in later stages and rolls back.

 

In this example,

  • T2 reads the dirty value of A written by the uncommitted transaction T1.
  • T1 fails in later stages and roll backs.
  • Thus, the value that T2 read now stands to be incorrect.
  • Therefore, database becomes inconsistent.

 

2. Unrepeatable Read Problem-

 

This problem occurs when a transaction gets to read unrepeated i.e. different values of the same variable in its different read operations even when it has not updated its value.

 

Example-

 

 

Here,

  1. T1 reads the value of X (= 10 say).
  2. T2 reads the value of X (= 10).
  3. T1 updates the value of X (from 10 to 15 say) in the buffer.
  4. T2 again reads the value of X (but = 15).

 

In this example,

  • T2 gets to read a different value of X in its second reading.
  • T2 wonders how the value of X got changed because according to it, it is running in isolation.

 

3. Lost Update Problem-

 

This problem occurs when multiple transactions execute concurrently and updates from one or more transactions get lost.

 

Example-

 

 

Here,

  1. T1 reads the value of A (= 10 say).
  2. T2 updates the value to A (= 15 say) in the buffer.
  3. T2 does blind write A = 25 (write without read) in the buffer.
  4. T2 commits.
  5. When T1 commits, it writes A = 25 in the database.

 

In this example,

  • T1 writes the over written value of X in the database.
  • Thus, update from T1 gets lost.

 

NOTE-

 

  • This problem occurs whenever there is a write-write conflict.
  • In write-write conflict, there are two writes one by each transaction on the same data item without any read in the middle.

 

4. Phantom Read Problem-

 

This problem occurs when a transaction reads some variable from the buffer and when it reads the same variable later, it finds that the variable does not exist.

 

Example-

 

 

Here,

  1. T1 reads X.
  2. T2 reads X.
  3. T1 deletes X.
  4. T2 tries reading X but does not find it.

 

In this example,

  • T2 finds that there does not exist any variable X when it tries reading X again.
  • T2 wonders who deleted the variable X because according to it, it is running in isolation.

 

Avoiding Concurrency Problems-

 

  • To ensure consistency of the database, it is very important to prevent the occurrence of above problems.
  • Concurrency Control Protocols help to prevent the occurrence of above problems and maintain the consistency of the database.

 

Next Article- Schedules in DBMS

 

Get more notes and other study material of Database Management System (DBMS).

Watch video lectures by visiting our YouTube channel LearnVidFun.

Constraints in DBMS | Types of Constraints in DBMS

Constraints in DBMS-

 

  • Relational constraints are the restrictions imposed on the database contents and operations.
  • They ensure the correctness of data in the database.

 

Types of Constraints in DBMS-

 

In DBMS, there are following 5 different types of relational constraints-

 

 

  1. Domain constraint
  2. Tuple Uniqueness constraint
  3. Key constraint
  4. Entity Integrity constraint
  5. Referential Integrity constraint

 

1. Domain Constraint-

 

  • Domain constraint defines the domain or set of values for an attribute.
  • It specifies that the value taken by the attribute must be the atomic value from its domain.

 

Example-

 

Consider the following Student table-

 

STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul A

 

Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.

 

2. Tuple Uniqueness Constraint-

 

Tuple Uniqueness constraint specifies that all the tuples must be necessarily unique in any relation.

 

Example-01:

 

Consider the following Student table-

 

STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul 20

 

This relation satisfies the tuple uniqueness constraint since here all the tuples are unique.

 

Example-02:

 

Consider the following Student table-

 

STU_ID Name Age
S001 Akshay 20
S001 Akshay 20
S003 Shashank 20
S004 Rahul 20

 

This relation does not satisfy the tuple uniqueness constraint since here all the tuples are not unique.

 

3. Key Constraint-

 

Key constraint specifies that in any relation-

  • All the values of primary key must be unique.
  • The value of primary key must not be null.

 

Example-

 

Consider the following Student table-

 

STU_ID Name Age
S001 Akshay 20
S001 Abhishek 21
S003 Shashank 20
S004 Rahul 20

 

This relation does not satisfy the key constraint as here all the values of primary key are not unique.

 

4. Entity Integrity Constraint-

 

  • Entity integrity constraint specifies that no attribute of primary key must contain a null value in any relation.
  • This is because the presence of null value in the primary key violates the uniqueness property.

 

Example-

 

Consider the following Student table-

 

STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
Rahul 20

 

This relation does not satisfy the entity integrity constraint as here the primary key contains a NULL value.

 

5. Referential Integrity Constraint-

 

  • This constraint is enforced when a foreign key references the primary key of a relation.
  • It specifies that all the values taken by the foreign key must either be available in the relation of the primary key or be null.

 

Read more- Foreign Key in DBMS

 

Important Results-

 

The following two important results emerges out due to referential integrity constraint-

  • We can not insert a record into a referencing relation if the corresponding record does not exist in the referenced relation.
  • We can not delete or update a record of the referenced relation if the corresponding record exists in the referencing relation.

 

Example-

 

Consider the following two relations- ‘Student’ and ‘Department’.

Here, relation ‘Student’ references the relation ‘Department’.

 

 

Student

STU_ID Name Dept_no
S001 Akshay D10
S002 Abhishek D10
S003 Shashank D11
 S004 Rahul D14 

 

Department

Dept_no Dept_name
D10 ASET
D11 ALS
D12 ASFL
D13 ASHS

 

Here,

  • The relation ‘Student’ does not satisfy the referential integrity constraint.
  • This is because in relation ‘Department’, no value of primary key specifies department no. 14.
  • Thus, referential integrity constraint is violated.

 

Handling Violation of Referential Integrity Constraint-

 

To ensure the correctness of the database, it is important to handle the violation of referential integrity constraint properly.

 

Next Article- Handling Violation of Referential Integrity Constraint

 

Get more notes and other study material of Database Management System (DBMS).

Watch video lectures by visiting our YouTube channel LearnVidFun.

Referential Integrity Constraint | Violation

Referential Integrity Constraint-

 

Before you go through this article, make sure that you have gone through the previous article on Constraints in DBMS.

 

We have discussed-

  • Relational constraints impose the restrictions on the database to ensure the correctness of data.
  • There are following 5 different types of relational constraints-

 

 

  • Referential Integrity constraint is enforced when a foreign key references the primary key of a relation.
  • It specifies that all the values taken by the foreign key must either be available in the relation of the primary key or be null.

 

Also read- Foreign Key in DBMS

 

Referential Integrity Constraint Violation-

 

There are following three possible causes of violation of referential integrity constraint-

 

Cause-01: Insertion in a referencing relation

Cause-02: Deletion from a referenced relation

Cause-03: Updation in a referenced relation

 

Cause-01: Insertion in a Referencing Relation-

 

  • It is allowed to insert only those values in the referencing attribute which are already present in the value of the referenced attribute.
  • Inserting a value in the referencing attribute which is not present in the value of the referenced attribute violates the referential integrity constraint.

 

Example-

 

Consider the following two schemas-

 

 

Here, relation “Student” references the relation “Branch”.

 

Student

Roll_no Name Age Branch_Code
1 Rahul 22 CS
2 Anjali 21 CS
3 Teena 20 IT

 

Branch

Branch_Code Branch_Name
CS Computer Science
EE Electronics Engineering
IT Information Technology
CE Civil Engineering

 

Here,

  • In relation “Student”, we can not insert any student having branch code ME (Mechanical Engineering).
  • This is because branch code ME is not present in the relation “Branch”.

 

Cause-02: Deletion from a Referenced Relation-

 

  • It is not allowed to delete a row from the referenced relation if the referencing attribute uses the value of the referenced attribute of that row.
  • Such a deletion violates the referential integrity constraint.

 

Example-

 

Consider the above two relations,

  • We can not delete a tuple from the relation “Branch” having branch code ‘CS’.
  • This is because the referencing attribute “Branch_Code” of the referencing relation “Student” references the value ‘CS’.
  • However, we can safely delete a tuple from the relation “Branch” having branch code ‘CE’.
  • This is because the referencing attribute “Branch_Code” of the referencing relation “Student” does not uses this value.

 

Handling the Violation-

 

The violation caused due to a deletion from the referenced relation can be handled in the following three ways-

 

Method-01:

 

  • This method involves simultaneously deleting those tuples from the referencing relation where the referencing attribute uses the value of referenced attribute being deleted.
  • This method of handling the violation is called as On Delete Cascade.

 

Method-02:

 

  • This method involves aborting or deleting the request for a deletion from the referenced relation if the value is used by the referencing relation.

 

Method-03:

 

  • This method involves setting the value being deleted from the referenced relation to NULL or some other value in the referencing relation if the referencing attribute uses that value.

 

Cause-03: Updation in a Referenced Relation-

 

  • It is not allowed to update a row of the referenced relation if the referencing attribute uses the value of the referenced attribute of that row.
  • Such an updation violates the referential integrity constraint.

 

Example-

 

Consider the above relation.

  • We can not update a tuple in the relation “Branch” having branch code ‘CS’ to the branch code ‘CSE’.
  • This is because referencing attribute “Branch_Code” of the referencing relation “Student” references the value ‘CS’.

 

Handling the Violation-

 

The violation caused due to an updation in the referenced relation can be handled in the following three ways-

 

Method-01:

 

  • This method involves simultaneously updating those tuples of the referencing relation where the referencing attribute uses the referenced attribute value being updated.
  • This method of handling the violation is called as On Update Cascade.

 

Method-02:

 

  • This method involves aborting or deleting the request for an updation of the referenced relation if the value is used by the referencing relation.

 

Method-03:

 

  • This method involves setting the value being updated in the referenced relation to NULL or some other value in the referencing relation if the referencing attribute uses that value.

 

Next Article- Closure of an Attribute Set

 

Get more notes and other study material of Database Management System (DBMS).

Watch video lectures by visiting our YouTube channel LearnVidFun.