开发者

memory management for static

开发者 https://www.devze.com 2023-01-22 13:21 出处:网络
where the static v开发者_JS百科ariables and methods are stored. in heap or stack and how these memories are released when not in useStatic variables are stored on the heap. I\'m not sure what you mean

where the static v开发者_JS百科ariables and methods are stored. in heap or stack and how these memories are released when not in use


Static variables are stored on the heap. I'm not sure what you mean by methods, but local variables to a method are stored on the stack.

The stack is released when you exit a method. When a method needs local variables, more stack is allocated. When the method exits, the memory is released.

The rest of memory goes on the heap, e.g. with the new operator. This is released once you release all references to an object:

Car myCar = new Car();

// Use your car.

myCar = null;

// Somewhere between this point and when your application
// exits, the memory will be released.

The same goes for static variables, but you specifically must set these to null if you want to make them eligible for garbage collection.

For more information on .NET memory management, Google search results on http://www.google.com/search?q=memory+management+.net+heap+stack gave a few nice articles.


This article gives a good explanation: static variables are stored on a special area of the heap in .net.

0

精彩评论

暂无评论...
验证码 换一张
取 消