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

What the difference between -Xss and -XX:ThreadStackSize ?



There's basically no difference between -Xss and -XX:ThreadStackSize, except
that the former dates before HotSpot became the default JVM in Sun's JDK,
where as the latter is an internal VM flag of HotSpot. In effect, the former
is support in a lot of other JVMs, but the latter is specific to HotSpot.

As an historical aside, the command line arguments for Sun JDK 1.0.x and
1.1.x had these:
-ss<number>       set the C stack size of a process
-oss<number>      set the JAVA stack size of a process

These arguments got deprecated by -Xss and -Xoss in later versions of JDK.

Non-internal flags either get processd by the java launcher (such as
-server, -client, -verbosegc, etc.) or get converted into VM internal flags
by HotSpot. See hotspot/src/share/vm/runtime/arguments.cpp for details.

For example, see this version:
the launcher part:
http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/f097ca2434b1/src/share/bin/java.c
lines 1052 - 1058,
and the VM part:
http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/81d815b05abb/src/share/vm/runtime/arguments.cpp
lines 2227 - 2240.

As you can see, -Xss gets converted into ThreadStackSize before the VM gets
to use it.
If the user did specify -ss or -Xss in command line arguments, that'll get
picked up directly by the launcher to run the main Java thread. Otherwise,
the launcher asks the VM for a preferred stack size, and HotSpot will return
ThreadStackSize, see here:
http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/81d815b05abb/src/share/vm/prims/jni.cpp,
lines 3286 - 3295.

That should explain how they've been implemented to be basically equivalent
in HotSpot.

One thing, though: because the -Xss/-XX:ThreadStackSize argument is handled
by the VM, *after* the primordial thread has been created, so they won't
cover the stack size of the primordial thread. In order to control stack
size correctly, Oracle/Sun's JDK doesn't run Java code in the primordial
thread. See this bug: http://bugs.sun.com/view_bug.do?bug_id=6316197.
If you ever felt the function "ContinueInNewThread" in the launcher was
weird, that's the fix for this issue, so that Java code doesn't get run in
the primordial thread in HotSpot.


resource from@ http://mail.openjdk.java.net/pipermail/hotspot-dev/2011-June/004272.html

Changing the default Java Stack size for JVM


In VM arguments, pass the following parameter.

Example: I want to change it to 4MB as my JVM stacksize.

-Xss4096K

more info @http://www.onkarjoshi.com/blog/209/using-xss-to-adjust-java-default-thread-stack-size-to-save-memory-and-prevent-stackoverflowerror/comment-page-1/#comment-135809