Thursday, December 6, 2012

immutable object



A classic example of an immutable object is an instance of the Java String class.

String s = "ABC";
s.toLowerCase();

The method toLowerCase() will not change the data "ABC" that s contains. Instead, a new String object is instantiated and given the data "abc" during its construction. A reference to this String object is returned by thetoLowerCase() method. To make the String s contain the data "abc", a different approach is needed.

s = s.toLowerCase();

Now the String s references a new String object that contains "abc". There is nothing in the syntax of the declaration of the class String that enforces it as immutable; rather, none of the String class's methods ever affect the data that a String object contains, thus making it immutable.
By default, fields and local variables are mutable. They can be made immutable using the final keyword.

int i = 42;
i = 43; // OK

final int j = 42;
j = 43; // does not compile

Primitive wrappers (Integer, Long, Short, Double, Float, Character, Byte, Boolean) are also all immutable. Immutable classes can be implemented by following a few simple guidelines


·      Don't provide "setter" methods — methods that modify fields or objects referred to by fields.

·      Make all fields final and private.

·      Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.

·      If the instance fields include references to mutable objects, don't allow those objects to be changed:

  Don't provide methods that modify the mutable objects.

   Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

Memory Information in UNIX


cat /proc/meminfo

·         MemTotal: Total usable RAM in kilobytes (i.e. physical memory minus a few reserved bytes and the kernel binary code)
·         MemFree: The amount of physical RAM left unused by the system.
·         Buffers: The amount of physical RAM used for file buffers.
·         Cached: The amount of physical RAM used as cache memory. Memory in the pagecache (diskcache) minus SwapCache.
·         SwapCache: This is the amount of Swap used as cache memory. Memory that once was swapped out, is swapped back in, but is still in the swapfile.
·         Active: The total amount of buffer or page cache memory, that is active. This part of the memory is used recently and usually not reclaimed unless absolutely necessary.
·         Inactive: The total amount of buffer or page cache memory that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes by the paging algorithm.
·         HighTotal: is the total amount of memory in the high region. The HighTotal value can vary based on the type of kernel used. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
·         LowTotal: The total amount of non-highmem memory.
·         LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory
·         SwapTotal: Total amount of physical swap memory.
·         SwapFree: Total amount of swap memory free.
·         Dirty: The total amount of memory waiting to be written back to the disk.
·         Writeback: The total amount of memory actively being written back to the disk.
·         Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. This parameter represents the worst case scenario value, and also includes swap memory.


vmstat : The performance monitoring command "vmstat" also gives lot of good information about the system memory. With "-s" option, vmstat displays a table of various event counters and memory statistics.