Java Memory Monitoring

Check the following code. See the graph below.


public class MemTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Memory Test");

		long longTest = 1;
		String sb = null;
		for (long lCount = 1; lCount < Long.MAX_VALUE; lCount++) {

			if (longTest <= 0) 	
			longTest = 1; 			
                        
                       longTest = longTest * lCount; 			
                       sb = "n" + sb + "MemoryTest loop==>" + lCount + " long==>"
					+ longTest;
			System.out.println("lCount==>" + lCount);
		}

	}

}

System will hang in next couple of minutes…if the memory consumption continuous like this.

Now the code is improved….


public class MemTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Memory Test");

		long longTest = 1;
		String sb = null;
		for (long lCount = 1; lCount < Long.MAX_VALUE; lCount++) {

			if (longTest <= 0) 				
                         longTest = 1; 			
                         
                          //Using this number to show difference in graph in beginning and later
                          if (lCount > 10000)
				sb = null;

			longTest = longTest * lCount;
			sb = "n" + sb + "MemoryTest loop==>" + lCount + " long==>"
					+ longTest;
			System.out.println("lCount==>" + lCount);
		}

	}

}

Monitor the application CPU / Memory while running for couple of hours (Min 24 hours to 48 hours) before going to release (QA/Production).
If the line is going up …it is going to blow up……

Thank you,
Bhavani P Polimetla

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s