Google
 

Wednesday, April 23, 2008

Performance Considerations While Calling Web Services from ASP.Net Page

Following are the considerations what affect performance while calling a web service from ASPX page:

1. HTTP two connections limit (not applicable for localhost case) as set under <connectionManagement >

Example:

<connectionManagement><add maxconnection="2" address="*" />
</connectionManagement>

2. Thread pool limits (IIS) as set under <processModel > and <httpRuntime >

Eaxmple:

<processModel minWorkerThreads="50" maxIoThreads="20"
maxWorkerThreads="20"/>


<httpRuntime minLocalRequestFreeThreads="4" minFreeThreads="8"
executionTimeout="90" />


Solution (1 & 2):

1. Tuning <connectionManagement>, <processModel> &
<httpRuntime> settings in Machine.config. Following are the attributes to understand:

  • maxWorkerThreads
  • minWorkerThreads
  • maxIoThreads
  • minFreeThreads
  • minLocalRequestFreeThreads
  • maxconnection
  • executionTimeout

Recommendations

The settings that are recommended in this section may not work for all applications. However, the following additional information may help you to make the appropriate adjustments. If you are making one Web service call to a single IP address from each ASPX page, Microsoft recommends that you use the following configuration settings:

  • Set the values of the maxWorkerThreads parameter and the maxIoThreads parameter to 100.
  • Set the value of the maxconnection parameter to 12*N (where N is the number of CPUs that you have).
  • Set the values of the minFreeThreads parameter to 88*N and the
    minLocalRequestFreeThreads parameter to76*N.
  • Set the value of minWorkerThreads to 50.

Remember, minWorkerThreads is not in the configuration file by default. You must
add it.

2. However, when you use this configuration, scenarios that involve one of the
following will probably use too many connections:

  • Requests are to multiple IP addresses.
  • Requests are redirected (302 status code).
  • Requests require authentication.
  • Requests are made from multiple AppDomains.

In these scenarios, it is a good idea to use a lower value for the maxconnection
parameter and higher values for the minFreeThreads parameter and the
minLocalRequestFreeThreads parameter.


3. When ASP.NET is running under IIS 6 in native mode, the IIS 6 process model
is used and settings in section are ignored. Please use the IIS administrative UI to configure things like process identity and cycling for the IIS worker process for the desired application

3. Hosting model

ASP.NET requires a host. On Windows Server™ 2003, the default host is
the Internet Information Services (IIS) 6.0 worker process (W3wp.exe). When you
use the ASP.NET Process Model, the host is the ASP.NET worker process
(Aspnet_wp.exe).


4. Cryptography

If your application only needs to ensure that information is not tampered with
during transit, you can use keyed hashing. Encryption is not required in this
case, and it is relatively expensive compared to hashing. If you need to hide
the data that you send over the network, you require encryption and probably
keyed hashing to ensure data validity. When both parties can share the keys,
using symmetric encryption provides improved performance in comparison to
asymmetric encryption. Although larger key sizes provide greater encryption
strength, performance is slower relative to smaller key sizes. You must
consider this type of performance and balance the larger key sizes against
security tradeoffs at design time.




More details on:

http://msdn2.microsoft.com/en-us/library/aa480507.aspx

http://msdn2.microsoft.com/en-us/library/ms998562(d=printer).aspx



Interview Questions

.NET


  1. How is it determined that variable is reference type or value type? Is it hard coded?

  2. Can value type also has methods just as reference types? Ans: Yes

  3. Can new keyword (operator) be used with value types?
    Ans: Yes. The new operator cannot be overloaded.
    If the new operator fails to allocate memory, it throws the exception, OutOfMemoryException.

  4. How can garbage collector call Finalize method though it is always protected?
    Ans: Uses reflector.


  5. How to tell GC not to move an object from one generation to next?


    Ans: Use fixed statement. The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context. Fixed can also be used to create fixed size buffers. The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of statement. Without fixed, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a fixed statement.

    // assume class
    // pt is a managed variable, subject to garbage collection.
    Point pt = new Point();
    // Using fixed allows the address of pt members to be
    // taken, and "pins" pt so it isn't relocated.
    fixed ( int* p = &pt.x ) { *p = 1; }

  6. How to call Grand Pa version of a method (virtual) from a grand child class?

  7. Difference between static & singleton class.

  8. How singleton class's static member instance() be remoted (static members can not be remoted).

  9. Is using Assembly.Load a static reference or dynamic reference?

  10. When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?

  11. Explain volatile keyword.

    Ans: The volatile keyword indicates that a field might be modified by multiple concurrently executing threads. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. The volatile keyword can be applied to these types:

    • Reference types.

    • Pointer types (in an unsafe context).

    • Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.

    • An enum type with an integral base type.

    • Generic type parameters known to be reference types.

    • IntPtr and UIntPtr.

    The type in question must be the field of a class or struct. Local variables cannot be declared volatile.


  12. A page has 5 links each pointing to a jpg file. When clicking over a link it should display picture with a string added to that dynamically. How to do that? Hint: iHttpHandler

  13. What is view state? Can view state store objects like dataset? Hint: Yes it can store dataset but not recommended.

  14. Can view state be stored as plain text instead of encrypted text?

Stacks and Heaps in .NET

Memory in .NET

A lot of confusion has been formed by people explaining the difference between value types and reference types as "value types go on the stack, reference types go on the heap". This is simply untrue (as stated) and this article attempts to clarify matters somewhat.

What's in a variable?

The key to understanding the way memory works in .NET is to understand what a variable is, and what its value is. At the most basic level, a variable is just an association between a name (used in the program's source code) and a slot of memory. A variable has a value, which is the contents of the memory slot it's associated with. The size of that slot, and the interpretation of the value, depends on the type of the variable - and this is where the difference between value types and reference types comes in.
The value of a reference type variable is always either a reference or null. If it's a reference, it must be a reference to an object which is compatible with the type of the variable. For instance, a variable declared as Stream s will always have a value which is either null or a reference to an instance of the Stream class. (Note that an instance of a subclass of Stream, eg FileStream, is also an instance of Stream.) The slot of memory associated with the variable is just the size of a reference, however big the actual object it refers to might be. (On the 32-bit version of .NET, for instance, a reference type variable's slot is always just 4 bytes.)
The value of a value type is always the data for an instance of the type itself. For instance, suppose we have a struct declared as:

struct PairOfInts
{
public int a;
public int b;
}


The value of a variable declared as PairOfInts pair is the pair of integers itself, not a reference to a pair of integers. The slot of memory is large enough to contain both integers (so it must be 8 bytes). Note that a value type variable can never have a value of null - it wouldn't make any sense, as null is a reference type concept, meaning "the value of this reference type variable isn't a reference to any object at all".

So where is stuff stored?

  • The memory slot for a variable is stored on either the stack or the heap. It depends on the context in which it is declared:
    Each local variable (ie one declared in a method) is stored on the stack. That includes reference type variables - the variable itself is on the stack, but remember that the value of a reference type variable is only a reference (or null), not the object itself. Method parameters count as local variables too, but if they are declared with the ref modifier, they don't get their own slot, but share a slot with the variable used in the calling code.
  • Instance variables for a reference type are always on the heap. That's where the object itself "lives".
  • Instance variables for a value type are stored in the same context as the variable that declares the value type. The memory slot for the instance effectively contains the slots for each field within the instance. That means (given the previous two points) that a struct variable declared within a method will always be on the stack, whereas a struct variable which is an instance field of a class will be on the heap.
  • Every static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created. (There don't need to be any instances created for that one slot to exist though.) Note that this heap is separate from the normal garbage collected heap - it's known as a "high frequency heap", and there's one per application domain.

An example

The above may all sound a bit complicated, but a full example should make things a bit clearer. Here's a short program which does nothing useful, but should demonstrate the points raised above.

using System;

struct PairOfInts
{
static int counter=0;

public int a;
public int b;

internal PairOfInts (int x, int y)
{
a=x;
b=y;
counter++;
}
}

class Test
{
PairOfInts pair;
string name;

Test (PairOfInts p, string s, int x)
{
pair = p;
name = s;
pair.a += x;
}

static void Main ()
{
PairOfInts z = new PairOfInts (1, 2);
Test t1 = new Test(z, "first", 1);
Test t2 = new Test(z, "second", 2);
Test t3 = null;
Test t4 = t1;
// XXX
}
}

Let's look at what's where in memory at the line marked with the comment "XXX". (Assume that nothing is being garbage collected.)

  • There's a PairOfInts instance on the stack, corresponding with variable z. Within that instance, a=1 and b=2. (The 8 byte slot needed for z itself might then be represented in memory as 01 00 00 00 02 00 00 00.)
  • There's a Test reference on the stack, corresponding with variable t1. This reference refers to an instance on the heap, which occupies "something like" 20 bytes: 8 bytes of header information (which all heap objects have), 8 bytes for the PairOfInts instance, and 4 bytes for the string reference. (The "something like" is because the specification doesn't say how it has to be organised, or what size the header is, etc.) The value of the pair variable within that instance will have a=2 and b=2 (possibly represented in memory as 02 00 00 00 02 00 00 00). The value of the name variable within that instance will be a reference to a string object (which is also on the heap) and which (probably through other objects, such as a char array) represents the sequence of characters in the word "first".
  • There's a second Test reference on the stack, corresponding with variable t2. This reference refers to a second instance on the heap, which is very similar to the one described above, but with a reference to a string representing "second" instead of "first", and with a value of pair where a=3 (as 2 has been added to the initial value 1). If PairOfInts were a reference type instead of a value type, there would only be one instance of it throughout the whole program, and just several references to the single instance, but as it is, there are several instances, each with different values inside.
  • There's a third Test reference on the stack, corresponding with variable t3. This reference is null - it doesn't refer to any instance of Test. (There's some ambiguity about whether this counts as a Test reference or not - it doesn't make any difference though, really - I generally think of null as being a reference which doesn't refer to any object, rather than being an absence of a reference in the first place. The Java Language Specification gives quite nice terminology, saying that a reference is either null or a pointer to an object of the appropriate type.)
  • There's a fourth Test reference on the stack, corresponding with variable t4. This reference refers to the same instance as t1 - ie the values of t1 and t4 are the same. Changing the value of one of these variables would not change the value of the other, but changing a value within the object they both refer to using one reference would make that change visible via the other reference. (For instance, if you set t1.name="third"; then examined t4.name, you'd find it referred to "third" as well.)