Sunday, September 17, 2006

General Talk about Java Memory

Basically Heap and Stack are the same thing. The difference is how they are used/accessed. They both are an area of memory where you store something. With a heap, you access what you are storing in any order you want and they stay there until you remove them.

With a stack, you normally access them in the reverse order you put them on the stack and they are automatically removed as you access them.


Both of 'em are sources from which memory is allocated. Stack is automatic, which is created when memory is in scope, and destroyed when it is out of scope. Heap is manual and created and destroyed when requested. On stack, the local variables (unless they are static or register data members), function parameters are stored on stack. On heap normally global variables, static local variables, any data allocated by using calls like new will be stored.

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

jre=jvm+some classes(like awt,swing...)

and if u wonder what is the use of providing jre...the answer is if u develop an application in java and u want to give it to the client then u also need to provide runtime environment also so jre is given for redistribution to the clients to whoom u make a s/w product...


jre= targeted for execution i.e. jvm+some classes(like awt,swing...)+runtime libraries.

jdk= mainly targeted for java development i.e. compilation (javac+others) + jvm + libraries.

u can find jvm in both jre and jdk.

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

We conclude our discussion of storage class and scope by breifly describing how the memory of the computer is organized for a running program. When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack.

When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(). If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack. Notice that the parameters passed by main() to func1() are also stored on the stack. If func1() were to call any additional functions, storage would be allocated at the new Top of stack. When func1() returns, storage for its local variables is deallocated, and the Top of the stack returns to to position. If main() were to call another function, storage would be allocated for that function at the Top. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.

The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.


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


ref : http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html


Runtime Data Areas
------------------

The Java virtual machine defines various runtime data areas that are used during execution of a program. Some of these data areas are created on Java virtual machine start-up and are destroyed only when the Java virtual machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

The pc Register
---------------

The Java virtual machine can support many threads of execution at once. Each Java virtual machine thread has its own pc (program counter) register. At any point, each Java virtual machine thread is executing the code of a single method, the current method for that thread. If that method is not native, the pc register contains the address of the Java virtual machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java virtual machine's pc register is undefined. The Java virtual machine's pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.

Java Virtual Machine Stacks
---------------------------

Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread.A Java virtual machine stack stores frames.it holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.

The Java virtual machine stack size limit may be set on virtual machine start-up using the "-oss" flag. The Java virtual machine stack size limit can be used to limit memory consumption or to catch runaway recursions.

The following exceptional conditions are associated with Java virtual machine stacks:


If the computation in a thread requires a larger Java virtual machine stack than is permitted, the Java virtual machine throws a StackOverflowError.

If Java virtual machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java virtual machine stack for a new thread, the Java virtual machine throws an OutOfMemoryError.

Heap
----

The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

A Java virtual machine implementation may provide the programmer or the user control over the initial size of the heap, as well as, if the heap can be dynamically expanded or contracted, control over the maximum and minimum heap size(start-up using the "-ms" and "-mx")

The following exceptional condition is associated with the heap:

If a computation requires more heap than can be made available by the automatic storage management system, the Java virtual machine throws an OutOfMemoryError.

Method Area
-----------

The Java virtual machine has a method area that is shared among all Java virtual machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in a UNIX process.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.

The following exceptional condition is associated with the method area:


If memory in the method area cannot be made available to satisfy an allocation request, the Java virtual machine throws an OutOfMemoryError.

Runtime Constant Pool
---------------------

A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file.
It contains several kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time. The runtime constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.

Native Method Stacks
--------------------

An implementation of the Java virtual machine may use conventional stacks, colloquially called "C stacks," to support native methods, methods written in a language other than the Java programming language.

Frames
------

A frame is used to store data and partial results, as well as to perform dynamic linking , return values for methods, and dispatch exceptions.

A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java virtual machine stack of the thread creating the frame. Each frame has its own array of local variables,its own operand stack, and a reference to the runtime constant pool of the class of the current method.

Note that a frame created by a thread is local to that thread and cannot be referenced by any other thread.


The ClassFile Structure
-----------------------

A class file consists of a single ClassFile structure:

ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}

Virtual Machine Start-up
------------------------
The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader. The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

Threads
-------
To synchronize threads the language uses monitors, a mechanism for allowing one thread at a time to execute a region of code. The behavior of monitors is explained in terms of locks. There is a lock associated with each object.

The synchronized statement performs two special actions relevant only to multithreaded operation:


After computing a reference to an object but before executing its body, it locks a lock associated with the object.

After execution of the body has completed, either normally or abruptly, it unlocks that same lock. As a convenience, a method may be declared synchronized; such a method behaves as if its body were contained in a synchronized statement.


Threads
-------

A variable is any location within a program that may be stored into. This includes not only class variables and instance variables, but also components of arrays. Variables are kept in a main memory that is shared by all threads. Because it is impossible for one thread to access parameters or local variables of another thread, it does not matter whether parameters and local variables are thought of as residing in the shared main memory or in the working memory of the thread that owns them.
Every thread has a working memory in which it keeps its own working copy of variables that it must use or assign. As the thread executes a program, it operates on these working copies. The main memory contains the master copy of every variable. There are rules about when a thread is permitted or required to transfer the contents of its working copy of a variable into the master copy or vice versa.

check the link : http://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.html
Book : The JavaTM Virtual Machine Specification, Second Edition

MS SQL SERVER PROCEDURE FOR TREE STRUCTURE

This MS SQL SERVER procedure is used to get the list of all children , childrens children so on... for a given parent

CREATE PROCEDURE TEST
@id numeric(10)
AS
begin
create table #temp(parentid numeric(10) , level int,process bit,root numeric(10))
-- 0 level
insert into #temp values(@id,0,0,0)
declare @level int
set @level=0
select top 1 @id = parentid from #temp where level = @level and process=0;

while(@id is not null)
begin
update #temp set process =1 where parentid = @id and level = @level and process = 0;
insert into #temp select GRP_ID_N,@level+1,0,@id from USR_GRPS where parent_grp_id_n = @id
select top 1 @id = parentid from #temp where level = @level and process=0;
if @@rowcount = 0
begin
set @id = null
set @level = @level + 1
select top 1 @id = parentid from #temp where level = @level and process=0;
end
end

select * from #temp

end

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]