Link to home
Start Free TrialLog in
Avatar of lordiano
lordiano

asked on

Memory Question

Say I have a class that exposes a static hashtable
Class A
{
private hashtable hash;
public static hashtable MyHashtable
{
get
{
return hash;
}
}
}

In my other class B
I want to access that very same hashtable.

I just call A.MyHashTable

Now my Question is that if I create a Hashtable instance in my Class B such as :

Class B
{
private Hashtable myHash = A.MyHashTable;
}

Will this actually copy the entire MyHashTable structure to Class B's memory stack, or it will just store a pointer to the actually memory address that contains MyHashTable.

Is doing this and then use
myhash["myKey"]
more performant than calling
A.MyHashTable["myKey"]  

??

Thanks
ASKER CERTIFIED SOLUTION
Avatar of BurntSky
BurntSky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would argue that this is a very dangerous design when it comes to thread safety as there is nothing protecting the hashtable which is NOT threadsafe from multiple threads accessing it concurrently.

i would also argue that the following code is impossible

"private hashtable hash;
public static hashtable MyHashtable
{
get
{
return hash;
}
}
}"

as the member hash is an instance member and the property is static ...

Greg

Avatar of lordiano
lordiano

ASKER

The private Hashtable hash; should be private static Hashtable hash;

My mistake for missing it.

I figured :)

in general I would be very wary of this design though as you lose all control of being able to lock the hash table (unless you create a threadsafe derived class for the hashtable)
How about if i do it this way

private static object syncObject = new object();

then in the public method to get my hashtable in Class A i do :

public static Hashtable MyHashTable
{
get
{
lock(syncObject)
{
return hash;
}
}
}

will this solve the thread issue?
I don't think so. I guess you meant lock(hash), anyway.
By doing that, what you do is ensure no two threads can 'get' the hashtable simultaneously.
However, that doesn't keep two objects from getting the hashtable one at a time, and then access it simultaneously.

To prevent that, you should create a class that encapsulates the HashTable and provides some thread safety.
A very basic implementation could be:

class MyHashTable {
    Hashtable H;
    public MyHashTable() {
        H = new Hashtable();
    }

    public object this[object key] {
        get {
            lock (H) {
                return H[key];
            }
        }
        set {
            lock (H) {
                H[key] = value;
            }
        }
    }

    public void Add(object key, object value) {
        lock (H) {
            this[key] = value;
        }
    }

    public int Count() {
        lock (H) {
            return H.Count;
        }
    }
}

Hope that helps.
Actually, I wont be doing any modification to the Hashtable. I will load up the necessary data the first time its being called and the content of the Hashtable will not be changed throughout the entire process.
Do i still need that?
If you're not going to change anything inside the Hashtable then you don't need to worry anything.  Locking is only so that other threads can't modify an object until the first one is done.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great, that sounds good.

By the way, I am initiating my Hashtable with ADO.NET from database.
That is, I am calling sproc and then use IDataReader and fill up my Hashtable with it.
The sproc simply returns a table of 2 columns, the 1st column would be the key and 2nd being the value.
This is how I am doing it now :

private static Hashtable hash;
private static object syncObject = new object();
public static hashtable MyHashtable
{
get
{
lock(syncObject)
{
if(hash == null)
{
hash = new Hashtable();
//Call sproc and use IDataReader to fill up the Hashtable
}
return hash;
}
}
}
}

I was wondering if instead of using the way I am doing now, can i do :

private static Hashtable hash = fillHashTable();

then I have a private function :

private Hashtable fillhashTable()
{
Hashtable resultHash = new Hashtable().Synchronized;
//Call the sproc and use IDataReader to fill up the Hashtable
}

and then finally in my Get i do

public static Hashtable Hash
{
get {return hash};
}


Is this a safe way of doing it??

Thanks alot again!

your static constructor is gaurenteed to e called before any other accessor ...

now does this information ever get deleted/changed?

Greg

No, it will be consistent throughout the running cycle of the program.
There will be multiple thread accessing this Hashtable, but all of them are just reading it.
Ops! I forgot to awarded the point.  Sorry about the delay, thanks a lot for your inputs.