2

My requirement is to pass some parameters from a function to another page. I considered two ways of doing it - 1)sessions , 2) Query strings.

Sessions are known to increase load on the server as apposed to query strings.

This is the way am extracting parameter values from the url(after using query string)

static string para1 ; static string para2;  
string[] parts = urlString.Split((new string[] { "?para1=", "&&para2="}), StringSplitOptions.None);

 para1 = parts[1];

 para2= parts[2];

Do these strings results in loading memory if I assign them to null after use? Is using session better than query strings in terms of memory allocation and server performance.

3
  • Does this help: programmers.stackexchange.com/questions/147713/… and stackoverflow.com/questions/6601446/… ? Commented Jun 3, 2015 at 7:49
  • 10
    You care about memory of two strings, are you micro-optimizing things? The problem with your static string variables is not the memory occupation but the fact that all users share them since they are static. Commented Jun 3, 2015 at 7:50
  • You should use Session.Remove(yourkey) to clean the space allocated in session Commented Jun 3, 2015 at 7:53

3 Answers 3

2

Firstly the memory used by these couple of strings is likely to be fairly small in the grand scheme of things, so probably not worth losing a lot of sleep over.

In terms of session vs query string, session variables will stay around for the duration of the user's session. Also consider whether the hosting environment can support session state.

Also your query string variables will stick around as part of the request object for the duration of the call anyway.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think that memory consumption is the main criteria upon which to decide whether to use query string parameters or session variables. In your example, the variables are static anyway so that the memory consumption should not be dependent on the number of users. Please note that static variables can have various unexpected effects in a web application as they are shared between all users.

In general, I'd prefer using query string parameters to transmit data to a certain page. Query string parameters are scoped to the request, so they are available to the requested page only whereas session variables can be accessed from any page.

Also, memory consumed by query string parameters is freed automatically after the request whereas you need to remove session variables if you don't need them anymore if you want to be very efficient as regards memory consumption. Sure, a session will time out if the user is not active anymore, but depending on your configuration this might result in wasting memory for half an hour.

No rule without exceptions: I'd use session variables instead of query string parameters if one of the following conditions is met:

  1. The data is security critical: session variables are stored on the server whereas query string parameters are transmitted to the client and are visible in the browser.
  2. If a large amount of data is to be transmitted to the other page it is also more efficient to keep the data on the server. In some browsers there is a limit on the length of a query string, so you sometimes cannot use this.
  3. The data is widely used in the application (widely as in "almost every page") so that the query string parameters had to be repeated over and over again.

Comments

1

Do static string variables occupy memory space even when null?

Technically, yes: all variables have at minimum the pointer size of the machine. As they are (currently) static, this pointer will remain until your site is shutdown by IIS idle timeout. But only one pointer per variable, no matter how many requests are made. Hardly worth the time considering.

If you set them to null after, the memory used by the content should be freed later by the GC, but the pointer will remain.

If you don't use static, then they will be freed as once they go out of scope.

Session always has an extra overhead (not least choosing options for its configuration) and you still need to get the values into session in the first place. If they're only used for a single request, then there's no need to put them in session (which is ideally used to store values across multiple requests).

Not the topic, but worth noting: Static variables are shared across all requests, so it's possible (likely?) if two requests are made at the same time then one will be getting the querystring parameters from the other request, more likely the longer processing takes after setting the values. Best advice is to make these non-static and simply recreate them as required or pass them around if needed in multiple methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.