Wednesday, May 15, 2013

JavaScript Learning #1 - undefined

Just for my reference!!

Very well explained about 'undefined' usage in JavaScript.  Source is from @Mozilla.

Examples:

Test#1:

function xx() { 
  var x; 
if (x == undefined) {
window.alert("undefined called"); 

else { 
window.alert("Not called");
}
}

xx();

output: "undefined called" window will popup.


Test#2:

function xx() { 
  var x = null; 
if (x == undefined) {
window.alert("undefined called"); 

else { 
window.alert("Not called");
}
}

xx();

output: "undefined called" window will popup.



Test#3:

function xx() { 
  var x = 10; 
if (x == undefined) {
window.alert("undefined called"); 

else { 
window.alert("Not called");
}
}

xx();

output: "Not called" window will popup.



Test#4:

function xx() { 
  var x = null; 
if (typeof x == undefined) {
window.alert("undefined called"); 

else { 
window.alert("Not called");
}
}

xx();

output: "Not called" window will popup.


------------------------------------------------------------------------------------------------------


Summary

The value undefined.
Core Global Property
Implemented inJavaScript 1.3
ECMAScript EditionECMAScript 1st Edition

Syntax

undefined 

Description

undefined is a property of the global object, i.e. it is a variable in global scope.
The initial value of undefined is the primitive value undefined.
Starting in JavaScript 1.8.5 (Firefox 4), undefined is non-writable, as per the ECMAScript 5 specification.
A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.
You can use undefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x is not defined, and the if statement evaluates to true.
1
2
3
4
5
6
7
var x;
if (x === undefined) {
   // these statements execute
}
else {
   // these statements do not execute
}
Note: The strict equality operator rather than the standard equality operator must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. null is not equivalent to undefined. See comparison operators for details.
Alternatively, typeof() can be used:
1
2
3
4
var x;
if (typeof x === 'undefined') {
   // these statements execute
}
One reason to use typeof() is that it does not throw an error if the variable has not been defined.
1
2
3
4
5
6
7
8
// x has not been defined before
if (typeof x === 'undefined') { // evaluates to true without errors
   // these statements execute
}
if(x === undefined){ // throws a ReferenceError
}
However, this kind of technique should be avoided. JavaScript is a statically scoped language, so knowing if a variable is defined can be read by seeing whether it is defined in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object (using the in operator, for instance)




Thursday, May 9, 2013

OutOfMemoryError: unable to create new native thread

Startup parameters of JVM and stacksize

Display all startup parameters:

>java -XX:+PrintFlagsFinal

This display all the startup parameters in the console.



To display the process ID:

C:\Users\kh1205>jps
3596
6616 Jps
1720 Main

Finding stack size:


C:\Users\kh1205>jinfo -flag ThreadStackSize  1720
-XX:ThreadStackSize=4096


4096 represents stack size in KB = 4 MB, which i have configured through 
-Xss4096K in VM arguments.



Default settings for the JVM

Info @ http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/index.jsp?topic=%2Fcom.ibm.java.doc.user.lnx.60%2Fdiag%2Fappendixes%2Fdefaults.html



JVM settingAIX®IBM® iLinuxWindowsz/OS®Setting affected by
Default localeNoneNoneNoneN/ANonee
Time to wait before starting plug-inN/AN/AZeroN/AN/Ae
Temporary directory/tmp/tmp/tmpc:\temp/tmpe
Plug-in redirectionNoneNoneNoneN/ANonee
IM switchingDisabledDisabledDisabledN/ADisablede
IM modifiersDisabledDisabledDisabledN/ADisablede
Thread modelN/AN/AN/AN/ANativee
Initial stack size for Java™ Threads 32-bit. Use:-Xiss<size>2 KB2 KB2 KB2 KB2 KBc
Maximum stack size for Java Threads 32-bit. Use: -Xss<size>256 KB256 KB256 KB256 KB256 KBc
Stack size for OS Threads 32-bit. Use-Xmso<size>256 KB256 KB256 KB32 KB256 KBc
Initial stack size for Java Threads 64-bit. Use:-Xiss<size>2 KBN/A2 KB2 KB2 KBc
Maximum stack size for Java Threads 64-bit. Use: -Xss<size>512 KBN/A512 KB512 KB512 KBc
Stack size for OS Threads 64-bit. Use-Xmso<size>256 KBN/A256 KB256 KB256 KBc
Initial heap size. Use -Xms<size>4 MB4 MB4 MB4 MB4 MBc
Maximum Java heap size. Use -Xmx<size>Half the available memory with a minimum of 16 MB and a maximum of 512 MB2 GBHalf the available memory with a minimum of 16 MB and a maximum of 512 MBHalf the real memory with a minimum of 16 MB and a maximum of 2 GBHalf the available memory with a minimum of 16 MB and a maximum of 512 MBc

Setting stack size to a Java thread

 From JavaDoc


java.lang.Thread.Thread(ThreadGroup group, Runnable target, String name, long stackSize)

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size.
This constructor is identical to Thread(ThreadGroup, Runnable, String) with the exception of the fact that it allows the thread stack size to be specified. The stack size is the approximate number of bytes of address space that the virtual machine is to allocate for this thread's stack.

The effect of the stackSize parameter, if any, is highly platform dependent.
On some platforms, specifying a higher value for the stackSize parameter may allow a thread to achieve greater recursion depth before throwing a StackOverflowError. Similarly, specifying a lower value may allow a greater number of threads to exist concurrently without throwing an OutOfMemoryError (or other internal error). The details of the relationship between the value of the stackSize parameter and the maximum recursion depth and concurrency level are platform-dependent. On some platforms, the value of the stackSize parameter may have no effect whatsoever.
The virtual machine is free to treat the stackSize parameter as a suggestion. If the specified value is unreasonably low for the platform, the virtual machine may instead use some platform-specific minimum value; if the specified value is unreasonably high, the virtual machine may instead use some platform-specific maximum. Likewise, the virtual machine is free to round the specified value up or down as it sees fit (or to ignore it completely).

Specifying a value of zero for the stackSize parameter will cause this constructor to behave exactly like the Thread(ThreadGroup, Runnable, String) constructor.

Due to the platform-dependent nature of the behavior of this constructor, extreme care should be exercised in its use. The thread stack size necessary to perform a given computation will likely vary from one JRE implementation to another. In light of this variation, careful tuning of the stack size parameter may be required, and the tuning may need to be repeated for each JRE implementation on which an application is to run.
Implementation note: Java platform implementers are encouraged to document their implementation's behavior with respect to the stackSize parameter.
Parameters:
group the thread group.
target the object whose run method is called.
name the name of the new thread.
stackSize the desired stack size for the new thread, or zero to indicate that this parameter is to be ignored.
Throws:
SecurityException - if the current thread cannot create a thread in the specified thread group.
Since:
1.4