Main Site Documentation

Memory problems?


#1

Hi! I’m developing an app that is using a timer in order to send “alive” messages. After some time, my timer stops working and show this error:

[quote]GC: 364msec 385548 bytes used, 12655500 bytes available
Type 0F (STRING ): 38136 bytes
Type 11 (CLASS ): 78444 bytes
Type 12 (VALUETYPE ): 4788 bytes
Type 13 (SZARRAY ): 213480 bytes
Type 15 (FREEBLOCK ): 12655500 bytes
Type 16 (CACHEDBLOCK ): 216 bytes
Type 17 (ASSEMBLY ): 22248 bytes
Type 18 (WEAKCLASS ): 144 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 504 bytes
Type 1D (OBJECT_TO_EVENT ): 408 bytes
Type 1E (BINARY_BLOB_HEAD ): 192 bytes
Type 1F (THREAD ): 3072 bytes
Type 20 (SUBTHREAD ): 240 bytes
Type 21 (STACK_FRAME ): 3084 bytes
Type 22 (TIMER_HEAD ): 72 bytes
Type 27 (FINALIZER_HEAD ): 18024 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 2256 bytes[/quote]

Here you can see how I defined timer:


public static void Main()
{
	...
	var thread1 = new Thread(AliveTimer);
	
	...
	thread1.Start();
	
	Thread.Sleep(Timeout.Infinite);
}

...

static void AliveTimer()
{
	System.Threading.Timer timer;
	timer = new System.Threading.Timer(new TimerCallback(MyCallBack), null, 0, 10000);
}

...

static void MyCallBack(object data)
{
	Debug.Print("Gateway ALIVE");
}

Thanks!!!


#2

Read this please http://wiki.tinyclr.com/index.php?title=C-Sharp_Level2#Garbage_Collector


#3

Why do you use a thread for that ? :o

Defining the timer is enough, it runs on its own thread.

So, remove the thread declaration and simply call


timer = new System.Threading.Timer(new TimerCallback(MyCallBack), null, 0, 10000);

once.

Edit: said something wrong, so I deleted it :-[


#4

Maybe the timer gets disposed by the GC after the AliveTimer thread has finished. Create timer on global scope, not in the scope of a thread.


#5

Yes that is it and this is why I pointed to GC tutorial which explains all this.


#6

Solved taking out timer of a thread.
Thanks to all!!