Reputation:
Is it kept allocated in memory after the method is called?
private void AnyMethod()
{
new AnotherClass().AnotherMethod();
}
Upvotes: 1
Views: 66
Reputation: 156978
Well, it depends on what is inside the constructor and that method, but usually it isn't.
When the method call ends, the instantiated class doesn't have any references any more, so it can be garbage collected whenever the GC comes around, which may vary given GC settings and memory pressure.
It is possible though that you do something like this in your constructor or method, which will prevent the instance to go out of scope:
SomeExternalClass.SomeInstance = this;
Upvotes: 2
Reputation: 1038840
After the method is called, the instance on which this method was called falls out of scope (because it has no longer any references pointing to it), so it is eligible to garbage collection. So this instance will be kept in memory until the actual garbage collection occurs. The exact timing when this garbage collection will occur will of course depend on many properties of the runtime, like memory usage, ...
Upvotes: 3