S
Stephen Brown
I have some strange behavior on my web server that seems to point to garbage
collection. I have a singleton that tracks web activity on my web site.
The singleton works great, except that it restarts periodically. The web
services have not been restarted and the error log shows no problems. It is
the same problem on 2 different servers, but is worse on the most used
server. On the most used server, it gets restarted 5 to 10 times a day
while the other server it is only restarted every couple of days. We
created a second singleton that gets instantiated at the same time to check
to see if was some web activity causing the restart and we found that it
also gets restarted at the same time as the first one. We also threw in
destructors that log to disk but they don't ever seem to get called.
Is a singlton subject to garbage collection and is there a clean way to
avoid this? All we need is a Singleton that will keep an instance as long
as the application is running.
Here's the singleton we used. What we see in the log file is multiple lines
instantiating the singleton but no destructor calls:
using System;
using System.IO;
using System.Configuration;
namespace ABC.DEF.SingletonTier.Tracking {
public class SingletonWatcher {
private static SingletonWatcher moInstance = null;
private static DateTime moStartTime;
private SingletonWatcher() {
this.WriteToLog("# SingletonWatcher Instantiate
#");
moStartTime = DateTime.Now;
}
~SingletonWatcher() {
try {
this.WriteToLog("# SingletonWatcher Destructor
#");
}catch(Exception) {
}
}
public static SingletonWatcher Instance {
get {
if(moInstance == null) moInstance = new SingletonWatcher();
return moInstance;
}
}
private StreamWriter moLogWriter = null;
public void WriteToLog(string sMessage) {
try {
if(moLogWriter == null) {
string sPath = "SingletonLog.txt";
FileStream oStream = new FileStream(sPath, FileMode.Append);
moLogWriter = new StreamWriter(oStream);
}
string sFormat = "dd-MM-yy HH:mm:ss";
string sOut =
"[Instance:"+moStartTime.ToString(sFormat)+"][Logged:"+DateTime.Now.ToString
(sFormat)+"] " + sMessage;
moLogWriter.WriteLine(sOut);
moLogWriter.Flush();
} catch(Exception) {
}
}
}
}
collection. I have a singleton that tracks web activity on my web site.
The singleton works great, except that it restarts periodically. The web
services have not been restarted and the error log shows no problems. It is
the same problem on 2 different servers, but is worse on the most used
server. On the most used server, it gets restarted 5 to 10 times a day
while the other server it is only restarted every couple of days. We
created a second singleton that gets instantiated at the same time to check
to see if was some web activity causing the restart and we found that it
also gets restarted at the same time as the first one. We also threw in
destructors that log to disk but they don't ever seem to get called.
Is a singlton subject to garbage collection and is there a clean way to
avoid this? All we need is a Singleton that will keep an instance as long
as the application is running.
Here's the singleton we used. What we see in the log file is multiple lines
instantiating the singleton but no destructor calls:
using System;
using System.IO;
using System.Configuration;
namespace ABC.DEF.SingletonTier.Tracking {
public class SingletonWatcher {
private static SingletonWatcher moInstance = null;
private static DateTime moStartTime;
private SingletonWatcher() {
this.WriteToLog("# SingletonWatcher Instantiate
#");
moStartTime = DateTime.Now;
}
~SingletonWatcher() {
try {
this.WriteToLog("# SingletonWatcher Destructor
#");
}catch(Exception) {
}
}
public static SingletonWatcher Instance {
get {
if(moInstance == null) moInstance = new SingletonWatcher();
return moInstance;
}
}
private StreamWriter moLogWriter = null;
public void WriteToLog(string sMessage) {
try {
if(moLogWriter == null) {
string sPath = "SingletonLog.txt";
FileStream oStream = new FileStream(sPath, FileMode.Append);
moLogWriter = new StreamWriter(oStream);
}
string sFormat = "dd-MM-yy HH:mm:ss";
string sOut =
"[Instance:"+moStartTime.ToString(sFormat)+"][Logged:"+DateTime.Now.ToString
(sFormat)+"] " + sMessage;
moLogWriter.WriteLine(sOut);
moLogWriter.Flush();
} catch(Exception) {
}
}
}
}