Wednesday, January 26, 2011

Garbage Collection and Object Size in Java

This is not one of those High Class profiling tools, but this class performs a simple function of creating and destroying a large number of instances of a Test class, and seeing how much memory is freed after every garbage collection call. Assuming that it does run on every request.
The code is pretty customizable, with all the parameters at the top of the code. The comments try to explain the working, but additional explanation is provided at the bottom of the code, wherever necessary.
 


/**


 * To test Garbage Collection , and Object Size


 */




import java.awt.Color;


import java.awt.Graphics;


import java.awt.image.BufferedImage;


import java.awt.image.RenderedImage;


import java.io.FileOutputStream;


import java.text.NumberFormat;


import javax.imageio.ImageIO;




/**


 * @author Bageshwar Pratap Narain


 * 


 */


public class GraphDemo {




/**


     * @param args


     */


public static void main(String[] args) {




/*


         * Graph Image Width


         */


int imW = 1200;




/*


         * Graph Image Height


         */


int imH = 800;




/*


         * The initial number of objects to check with.


         */


long initValue = 10000;




/*


         * The increment value to plot the graph. The <code>incValue</code> is


         * added to <code>initValue</code> until it reaches <code>limit</code>


         */


long incValue = 10000;




/*


         * The Upper limit of the number of objects to check with.


         */


long limit = 120000;




/*


         * Image Type. Exceptable values are "jpg", "png"


         */


String imageType = "jpg";




/*


         * The complete path where the graph image will be saved. Take care of


         * directory separator in Linux vs Windows yourself.


         */


String imgPath = "d:\\graph.jpg";




/*


         * The Radius of the point that is drawn on the graph.


         */


int pointRadius = 6;




/*


         * Initializing Graph Image


         */


BufferedImage bi = new BufferedImage(imW, imH,


BufferedImage.TYPE_INT_RGB);


Graphics g = bi.getGraphics();




/*


         * Setting the background to white. Strangely it was black by defualt.


         */


g.setColor(Color.WHITE);


g.fillRect(0, 0, imW, imH);


g.setColor(Color.BLACK);


// now the image writer is ready to create the graph.




/*


         * An array of [Number of Objects][Average size of Objects].


         */


float data[][] = new float[(int) ((limit - initValue) / incValue)][2];


int temp = 0;




// get all the doRun values and need the min and max values also.


float min = Float.MAX_VALUE, max = Float.MIN_VALUE;




/*


         * Runs a method, which creates "n" number of objects at a time and


         * garbage collects them. The average size of the object is calculated.


         * The loop works on <code>initValue,incValue and limit</code> . The


         * maximum and minimum average object sizes are also calculated in the


         * same loop.


         */


for (long i = initValue; i < limit; i = i + incValue) {


data[temp][0] = i;


data[temp][1] = doRun(i);




if (data[temp][1] >= max)


max = data[temp][1];




if (data[temp][1] <= min)


min = data[temp][1];




temp++;


}




/*


         * x_files is the scale factor, which is used to expand the Y-axis to


         * better visualize the graph, by only concentrating on the concentrated


         * part of the graph.


         */


float x_files = (max - min) * 1.3f; // taking a 30% margin




/*


         * System.out.println("Min : " + min); System.out.println("Max : " +


         * max); System.out.println("X : " + x_files);


         */




/*


         * Used internally to draw the line graph.


         */


int initX = 0, initY = 0;




/*


         * Used internally with the scale factor on the Y-axis.


         */


float unit = imH / x_files;




/*


         * 


         * To format the Average Object 


         * size float value


         */


NumberFormat nf = NumberFormat.getNumberInstance();


nf.setMaximumFractionDigits(5);


nf.setMinimumFractionDigits(5);


 


for (int i = 0; i < temp; i++) {


float r = data[i][1];




/*


             * X and Y co-ordinates are calculated based on the simple arithmatic


             * function.


             */


int x = (int) ((data[i][0]) / (limit / imW));


int y = (imH - (int) ((r - min) * unit));


// System.out.println(r + ":" + x + ":" + y);




// Drawing the Point.


g.fillOval(x, y, pointRadius, pointRadius);




/*


             * Drawing the point connecting line.


             */


if (!(initX == 0 && initY == 0))


g.drawLine(initX, initY, x, y);


/*


             * Translating points for the next line to be drawn.


             */


initX = x;


initY = y;




/*


             * Labelling the graph logically.


             */


g.drawString(nf.format(r) +" @ " + (int)(data[i][0]), x, y);


}



try {


/*


             * Finally writing the Image data to the file.


             */


ImageIO.write((RenderedImage) bi, imageType, new FileOutputStream(


imgPath));


} catch (Exception e) {


e.printStackTrace();


}




}




/*


     * @param o O is the number of objects to test. @return Average Size of


     * Object in Bytes.


     */


private static float doRun(long o) {


Runtime r = Runtime.getRuntime();




long noOfObjects = o;




/*


         * Cleaning up the memory before the experiment.


         */


System.gc();




long memSize = r.freeMemory();




/*


         * Creating Bogus objects.


         */


for (long i = 0; i < noOfObjects; i++) {


/*


         * This is the Object under experiment. 


         * Change this to the Object of your choice.


         */


new Test();


}


long finalSize = r.freeMemory();


System.gc();




/*


         * Calculating the Average Object Size


         */


float val = ((memSize - finalSize + 0.0f) / noOfObjects);




return val;


}




}




/*


 * The Test Candidate Class.


 */


class Test {




}




 



Explanation



The initial image dimension are suitable for a normal graph, but if you are testing over a large range of objects, you should increase the width. Height is not that important since the estimated size of object does not change that much.


The initValue, incValue and limit are used to create “initvalue” number of objects, and then increasing the number by “incValue” until it reaches “limit”.


The output on the graph is


Average Object Size @ [Number of Objects created]


The lines of the graph are not that neat, since no anti-aliasing is used neither is planned to!


Drop me a mail if you have any more suggestions.

Friday, January 21, 2011

Long File/Directory path in Windows

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string<NUL>" where "<NUL>" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Note  File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections.

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".

Note  The maximum path of 32,767 characters is approximate, because the "\\?\" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

The "\\?\" prefix can also be used with paths constructed according to the universal naming convention (UNC). To specify such a path using UNC, use the "\\?\UNC\" prefix. For example, "\\?\UNC\server\share", where "server" is the name of the computer and "share" is the name of the shared folder. These prefixes are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory. Because you cannot use the "\\?\" prefix with a relative path, relative paths are always limited to a total of MAX_PATH characters.

There is no need to perform any Unicode normalization on path and file name strings for use by the Windows file I/O API functions because the file system treats path and file names as an opaque sequence of WCHARs. Any normalization that your application requires should be performed with this in mind, external of any calls to related Windows file I/O API functions.

When using an API to create a directory, the specified path cannot be so long that you cannot append an 8.3 file name (that is, the directory name cannot exceed MAX_PATH minus 12).

The shell and the file system have different requirements. It is possible to create a path with the Windows API that the shell user interface is not be able to interpret properly.