Freeing Variables

I like to create my own wallpaper changer. I know that there is a lot on the Internet, but I'm just trying to learn new things. So, so far, every time I created any simple program, I did not care about RAM / Memory, because I basically created programs for the school, and it was like a one-time program, and then I forgot about it .

But now I'm trying to create an application that I would like to use, something of mine. I noticed that my program takes about ~ 4000 thousand. In the "alt + ctrl + del" window, and sometimes it takes up to 200 000k when it changes the wallpaper, and sometimes it decreases and sometimes remains high until it changes to another . Memorytaken

So, the question arises: what are the tips for my application to use the least possible ram while working (tray icon, and the main windows are hidden using if (FormWindowState.Minimized == WindowState) Hide(); )

Is a variable inside a function occupying any memory? Example

 int function(int a){ int b = 0; int c = a+b; return c; } 

Or are these variables freed after the function returns some value?

I could use some tips, guides and / or links to articles where I could get some information about this. Friendly is yours.

EDIT: Okay, I read something, started disposing of bitmap images, got rid of one of my global variables that I used .. and its on a stable 4000-7000k now. Raising a little when changing the wallpaper, but then dropping to it. Therefore, I assume that this is a success for me. One more thing left. I downloaded kinda big / large / with many optional programs that change the wallpaper, and it has more features than mine, and yet it takes about 1000-2000 thousand, so ima is now reading that it can take so many "rams " in my. Right when I run my program around 4100, I think I can still do something to optimize it. Thanks everyone for the answers! :)

+4
source share
3 answers

The memory from your program perspective is divided into two blocks, if you do. Stack and heap.

Stack represents the current execution frame (for example, the currently executing function), and it is used to pass function parameters, return values, and local variables that are usually stored. This memory is cleared when the current execution frame ends (for example, your function exits).

A heap is a memory pool in which objects can be created and stored for longer periods of time. As a rule, everything created using the β€œnew” operator will be displayed on the heap with links on the stack (for the local context). If references to the selected object cease to be used, this memory is stored until the garbage collector runs at some indefinite time in the future and frees memory. When the launch of the GC cannot be guaranteed - it can be when your program runs out of memory or at certain intervals, etc.

I think that in the memory behavior that you observe, bursts are associated with opening and loading resources, deflections after the GC are triggered. Another way to notice this is to look at the program memory area when the user interface is displayed on the screen, as well as when the program is minimized. By minimizing the amount of memory will decrease, because all graphic elements are no longer needed. When you maximize the user interface and redraw it, peaks in memory usage.

You can see the following articles for a better understanding of the stack and heap:

C # Stack and Heap

What is stack and heap?

You can also look at the garbage collection:

Garbage Collection Article on MSDN

... and Meaning vs Link Types

+4
source

Make sure you use block usage around everything that implements the iDisposable interface. In particular, if you are reading files, any streams or any requests. You can read a little about it at http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx , and it gives some examples of how to use it.

+3
source

Memory for locally declared variables will be automatically released.

The memory obtained for variables that will be stored outside the function will also be released when they are no longer used, by something called GarbageCollector (GC for short).

So do not worry, you are not creating a memory leak using your sample function.

It’s hard to tell you where you could spend 200,000 yen. There are profilers that can help (I don’t have anyone to recommend, but first of all it is on Google: http://memprofiler.com/ )

+1
source

Source: https://habr.com/ru/post/1392819/


All Articles