Should variables be reused to optimize resource usage?

I use Microsoft Visual C # 2010. I have several methods that use a large raster map for local processing, and each method can be called several times.
I can declare a global variable and reuse it:

Bitmap workPic, editPic; ... void Method1() { workPic = new Bitmap(editPic); ... } void Method2() { workPic = new Bitmap(editPic.Width * 2, editPic.Height * 2); ... } 

or declare a local variable in each method:

 Bitmap editPic; ... void Method1() { Bitmap workPic = new Bitmap(editPic); ... } void Method2() { Bitmap workPic = new Bitmap(editPic.Width * 2, editPic.Height * 2); ... } 

The second way is better for code clarity (local variables for local use). Is there any difference in the use of resources?

+5
source share
3 answers

If you intend to save the allocated memory, you can use workPic again after the method, you must register it as a class variable. If not, you can free the memory (always a good idea) by letting it go out of scope.

The allocation of one variable does not matter much for the structure that manages the memory. Only if you re-create the variable inside the hard loop can you reuse the variable. If you have basic types, you even reuse the same memory. In addition, only a link to the allocated memory is saved, so there is not much benefit from it.

Note that Dispose your workPic is very important, as you now have a memory leak in unmanaged memory behind the Bitmap . It is preferable to use using .

+3
source

Why global variables should be avoided when unnecessary

Nonlocality . Source code is easiest to understand when the volume of its individual elements is limited. Global variables can be read or modified by any part of the program, making it difficult to remember or reason about every possible use.

No access control or restriction checking . A global variable can be obtained or set by any part of the program, and any rules regarding its use can be easily broken or forgotten. (In other words, get / set Accessors are usually preferable to direct access to data, and this is even more so for global data.) In addition, the lack of access control significantly impedes security in situations where you may want to run untrusted code (for example, working with third-party ones) plugins).

Implicit link . A program with many global variables often has tight connections between some of these variables and couplings between variables and functions. Grouping related items into connected units usually leads to better programs.

Concurrency returns - if global global objects can be accessed by multiple threads of execution, synchronization is necessary (and too often neglected). When dynamically linking modules to global ones, a compiled system can be unsafe, even if two independent modules tested in dozens of different contexts were safe.

Namespace pollution . Global names are universally available. You can unconsciously end up using global when you think you are using local (spelling or forgetting to declare local) or vice versa. Also, if you need to bind modules, the same global variable name, if you're lucky, you'll get an error link. If you're out of luck, the linker will simply consider all uses with the same name as the same object.

Memory allocation problems - Some environments have memory allocation schemes that make global allocation tricks. This is especially true in languages ​​where "constructors" have side effects other than distribution (because in this case you can express unsafe situations where two global characters are mutually dependent on each other). In addition, when dynamically linking modules, it may not be clear whether different libraries have their own instances of global or shared global ones.

Testing and confinement - a source that uses global variables, is a little harder to verify because you cannot easily set up a "clean" environment between runs. More generally, a source that uses global services of any type (for example, reading and writing files or databases) that are not explicitly provided to this source is difficult to verify for the same reason. For communication systems, the ability to test system invariants may require the launch of more than one “copy” of the system at a time, which greatly complicates any use of shared services - including global memory - that are not intended to be shared as part of the test.

link: http://c2.com/cgi/wiki?GlobalVariablesAreBad

+3
source

The main thing is to understand here that the field and the variable contain only a link, memory will be allocated to the object (s) created by the "new". Therefore, in both cases, all created raster image objects must undergo garbage collection.

The difference is that the object only specified in the method will be ready for collection immediately after the method is executed, when the object that still has a link in the field will be ready for collection only when the object containing the field is also ready for collect.

The only time it makes sense to enter this field is when you use the same object in the life cycle of the host object.

In cases where you recreate an object at the beginning of a method, a specific variable is recommended.

+1
source

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


All Articles