Java Memory Management

Posted in Java Development by admin - Dec 24 2017

Java memory management

In languages like c, c++ memory management is done in explicitly by the developer. A small mistake in manually emptying the allocated memory may lead into memory leak. Then for applications that handle enormous amount of data, may run out of memory and lead to crash. But Java provides a mechanism for memory management. Garbage collection is the term used to describe automatic memory management in Java. Heap is the place where garbage collector works. Heap is part of memory where Java objects live. Hence garbage collector maintains as much free space in heap.

An object goes out of scope or it is eligible for deletion, when no living thread can access the object. Garbage collector is in control of Java Virtual Machine (JVM).If an object goes out of scope; we cannot ensure that immediately the garbage collector runs. JVM decides when to run Garbage collector as it senses that memory is running low.

When we write codes we can make objects eligible for garbage collection explicitly. One way is nulling references; means we remove references to objects by assigning that references to null. Then that object is eligible for garbage collection if no more references exist to that object in any live thread. The next way is to re assigning reference variables. Consider that we have two reference variables of Stringbuffer sb1 contains ‘hello’ and sb2 contains ‘world’. If we assigns sb1 = sb2 then the reference sb1 redirects to sb2 and the Stringbuffer ‘hello’ is eligible for garbage collection. Also we can make code eligible for garbage collection by isolation of a reference variable. For example if a class has instance variable which is a reference variable to another instance of the same class. Let such two instances refer to each other and all other references to these two object are removed. In this situation both the objects has valid reference, but no live thread can access either of the object. In this case also the garbage collector can find out this isolated set of objects and removes it.

We may make ‘requests’ not ‘demands’ explicitly to garbage collector. In order to do this we have to just include/use where we want “System.gc();”.It is just a request, the JVM handles the request. The protected void finalize() method something called just before the object is deleted. If an object is holding some non-Java resources like file handle, we can ensure by putting code into finalize().Even though there is automatic garbage collection, the system may run out of memory if we handle too many live objects. This indicates that garbage collection can only efficiently manage the available memory.

 

Introduction to Layout Managers in Swing

            Java Swing is used for a variety of web based enterprise applications due to its user friendly layouts, features like copy paste from excel sheets, word, easy calculations on the user web page etc. Swing basically consists of components ranging from the very basic components, like a text field, to some very rich text documents, components with heavy styling properties, tables and so on. This helps us to builds a user interface with a modern and elegant presentation. One of the main concerns of a developer is regarding the layout and placement of components in a way to meet the specified goals. Swing helps them with it by providing several efficient layout managers.

           There are two interfaces included in the AWT package namely LayoutManager or its subclass, LayoutManager2. LayoutManager and all the layout managers should implement either of them. These interfaces provide structured and organized methods to manage layouts, positioning and sizes of the components. So layout managers are classes which implements either LayoutManager or LayoutManager2 and these classes are used to create the custom presentation of the page. Some of them are CardLayout, GridBagLayout, BoxLayout, etc.

           One of the basic layouts is BoxLayout. It lays component either along the horizontal axis or vertical of the mentioned container and manage the positions itself according to the components preferred sizes. An upgraded BoxLayout is a class called Box. Box represents a container with its layout manager as BoxLayout by default. One of the extra features of the Box layout is the provision of inserting invisible spaces, which are blocks without a view but has other properties like position and size. One of those invisible components is filler. Filler can be used in a box layout with three different specifications by calling any of the three methods provided by the class Box. One method returns filler which will fill all the available gaps between the components. The second method returns one with a specified height and the last method returns the invisible component with the specified height and width.

          A FlowLayout is a very simple layout which lays components next to each in a horizontal plane, in a left-right row-row fashion. This layout purely depends on the width of the layout and hence a desirable components position is not attainable.

          Another layout called GridLayout assigns a rectangular grid for each component. For a normal GridLayout, this rectangular grid would have a dimension of one column and one row. We could also specify number of rows and columns for each component and the gap size between rows and columns as well. It forces every component to occupy the grid which could result in undesirable component enlargement or shrinking.

           BorderLayout treats the container as a plane with four borders (north, south, west and east) and a central region. A components position can be specified using the static fields of the class BorderLayout: BorderLayout.EAST, etc.

           One of the most powerful layouts is GroupLayout. The core design principle of this layout manager is dimension independence which means that it treat the components in horizontal and vertical dimensions differently. And while defining layout in each dimensions, the components are further divided into groups which can be sequential or parallel and can contain gaps, components and other groups. Each component should be mandatory defined in each dimension otherwise GroupLayout Exception will be thrown by the compiler.

           There are two types of groups in which these components are packed into: sequential and parallel. In a sequential group, components are placed in a line one after the other either along one axis, just like other layouts in which components are placed along one axis. And in a parallel group, components are placed on top of the other with reference to a common point, which can be again either along the horizontal axis (right-aligned) or along the vertical axis (bottom-aligned). That means a sequential group in one dimension would be a parallel group in the other.

GroupLayout in Java Swing

Posted in Java Frameworks by admin - May 09 2015

Introduction to GroupLayout in Java Swing

            GroupLayout is a very efficient layout manager developed by the Swing Labs project team and was primarily deployed to meet the GUI builder requirements but since it is heavily reliable, it is widely used in manual coding as well.

           The core design principle of this layout manager is dimension independence which means that it treat the components in horizontal and vertical dimensions differently. And while defining layout in each dimensions, the components are further divided into groups which can be sequential or parallel and can contain gaps, components and other groups. Each component must be defined in each dimension otherwise GroupLayout Exception will be thrown by the compiler.

           There are two types of groups in which these components are packed into: sequential and parallel. In a sequential group, components are placed in a line one after the other along one axis, just like other layouts in which components are placed along the same axis. And in a parallel group, components are placed on top of the other with reference to a common point, which can be either along the horizontal axis (right-aligned) or along the vertical axis (bottom-aligned). That means a sequential group in one dimension would be a parallel group in the other.

So the design objective is to define the components in groups and to combine each dimension. Let’s take a simple example. Consider three components arranged in a line.

A     B     C

First step is to define them in each dimension. In the horizontal dimension, there exists one sequential group and in the vertical dimension, there exists one parallel group aligned along the baseline.

Horizontal Layout = sequential group {A, B, C}

Vertical Layout = parallel group (BASELINE) {A, B, C}

Let’s add one more component,

A      B     C 1

                     C2

Now there is a new parallel group (C1, C2) along the horizontal axis and along the vertical axis, there is a new sequential group (C1, C2)

Horizontal layout = sequential group {A, B, parallel group (LEFT) {C1, C2}}

Vertical layout = sequential group {parallel group (BASELINE) {A, B, C1}, C2}

An extra feature offered by the GroupLayout is Gaps. Gaps can be defined as the space between components in the layout. By default GroupLayout defines a preferred space between components. This default gap can be utilized by calling setAutocreateGaps(true) for the corresponding layout. There is a provision to add gaps as a component in a group by invoking methods like addPrefferedGap() and addContainerGap(). We can also set our component gaps by passing the value as a parameter to these methods. There are three constants defined to specify the gap layout namely RELATED, UNRELATED and INDENT. Related specifies a standard gap between components and unrelated represents a much bigger gap than related. Constant indent represents the horizontal gap between the parallel components where the second component (the bottom one) will be placed with the specified indent.

The actual code for the layout of the above example is as shown below:

GroupLayout gl = new GroupLayou(JPanel);

gl.setHorizontalGroup(layout.createSequentialGroup()

.add(A)

.add(B)

.add(gl.createParallelGroup(GroupLayout.LEADING)

.addC1)

.add(C2)));

layout.setVerticalGroup(layout.createSequentialGroup()

.add(layout.createParallelGroup(GroupLayout.BASELINE)

.add(A)

.add(B)

.add(C1))

.add(C2)

);