|
I was testing the garbage collection in C#, and came across something that doesn't make sense to me. I know about Garbage Collection happening "randomly", but I am using GC.Collect() here. Please see code below and then the result information:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Method();
Method2();
}
void Method()
{
TestA b = new TestA("first one");
for (int i = 0; i < 10; i++)
{
TestA a = new TestA();
}
GC.Collect();
}
void Method2()
{
}
}
public class TestA
{
string MSG;
public TestA()
{
MSG = "Not first one";
}
public TestA(string msg)
{
MSG = msg;
}
~TestA()
{
MessageBox.Show("Destructing A " + MSG);
}
}
I am getting "Not first one" (destructor and I am assuming GC'ed) 9 times. Then when I close the form I am getting "first one" 1 time and then "Not first one" 1 more time.
My main question is why isn't instance b of TestA being collected on GC.Collect? Are the ones in the for loop falling out of scope, but not the one before my for loop? If so, please explain what is keeping it in scope as Method() has completed.
Thank you.
Respectfully,
D
|
|
|
|
|
DSLoginYea wrote: My main question is why isn't instance b of TestA being collected on GC.Collect? It's no longer in scope, but that doesn't guarantee that it'll be collected.
If you want to know more about Garbage Collection, there's a lot of documentation[^] on it, including it's inner workings and the reasoning on the design-decisions.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Calling GC.Collect() does not mean the GC will run instantly.
Its only an indication that the GC can be run.
|
|
|
|
|
Add a GC.WaitForPendingFinalizers() after GC.Collect() - then the results may come closer to what you expected.
|
|
|
|
|
Hi All,
I am trying to run an existing project by using the F5 option. since this project is set up as a service, it keeps erroring out with message, "Cannot start service from command line or debugger. A windows service must first be installed then started with Server Explorer, windows services admin tool or NET Start Command".
I did google this but comes back with a lot of results. Did someone go through, and if so can you please let me know how you resolved it?
I am new to the windows service in ASP.Net.
Thanks so much.
|
|
|
|
|
|
I do something like the following. Basically you need to have a windows form or WPF app ready to run. You can do this by just adding one to your project. You comment the service and uncomment the forms code to run in VS. Put it back to run as a service. If this isn't already set for you then you have some work to do to make it run, but it shouldn't be hard. This code is in the Program.cs file
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new myNewService() };
ServiceBase.Run(ServicesToRun);
}
}
|
|
|
|
|
|
Hi Team,
Below i faces issues in String using RichText box control
for example :
Hi Welcome website like www.google.com and visit this site lot of things
when i bind this strings in our RichText box control , unable mid word like hyberlink (www.google.com)
please give your suggestion and needful
thanks
senthil
|
|
|
|
|
|
I want to design gridview inside another gridview with insert update and delete
I design database with tables are
1) tbl_Show(ShowID{P},ShowTime,Silver,Gold, Platinum,ShowTimeId) 2) tbl_STime(ShowTimeId{P},TheatreId,MovieId,FromDate,ToDate) 3) tbl_Theatre(TheatreId{P}, TName, Taddress)
I want to design same in asp.net C# like this visit http://www.codeproject.com/Articles/14293/Gridview-Inside-a-GridView-in-ASP-NET-2-0
|
|
|
|
|
Ok so what is stopping you, you have made lots of statements but have not asked a question. As you have already found an example of what you want why not try adapting that to your need?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
you have got your answer. just follow your given example and try to make your requirement. Don't worry
|
|
|
|
|
I am working on new project and i want to make a backup for my database and restore it again using c# but i don`t want to install SQL server on pc`s client.
So, i need to work on two points:
1- How to protect this database file ".mdf" with password.
2- Backup and Restore for this single file ".mdf"
Can any one help me?
NOTE: I DON`T WANT TO INSTALL SQL SERVER FULL Version, Just lite one to UNDERSTAND .mdf FILE
eng. Awad A. Bekhet
BSc. of Computer Engineering
Embedded Systems Engineer
htttp://about.me/AwadBekhet
modified 3-Aug-13 12:01pm.
|
|
|
|
|
for password protection you can use MEDIAPASSWORD option
e.g. backup database abc to disk='c:\abc.bak' with MEDIAPASSWORD='1234'
and for you other question have to check.. 
|
|
|
|
|
thanks for reply
but i am using smo class not sql query
|
|
|
|
|
|
Thanks
I had already did it, backup and restore, but there is must be a server (installed sql sever) , can i do this with no server.... 
|
|
|
|
|
I am curious how you are managing to use SQL Server without installing SQL Server?
|
|
|
|
|
I mean NO SQl SERVER MANAGEMENT STUDIO(FULL Version), off course, lite version will be installed to understand .mdf file
|
|
|
|
|
Make your mind up. Here you state it's just Management Studio you don't want to install, yet you state above that you don't want to install any Sql Server.
|
|
|
|
|
awad bekhet wrote: NOTE: I DON`T WANT TO INSTALL SQL SERVER You can't use Sql Server without installing it. Try Microsoft Access.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I mean NO SQl SERVER MANAGEMENT STUDIO(FULL Version), off course, lite version will be installed to understand .mdf file
|
|
|
|
|
There is no Sql Server Lite. Sql Server needs a full database-server to operate. SQLite is a different database-format altogether.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
That version which was been installed with visual studio, whats its name ? I mean it.
|
|
|
|