473,511 Members | 17,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Will not unboxing cause memory leaks?

10 New Member
Is memory leaks possible if a value type is boxed and is not unboxed?

Expand|Select|Wrap|Line Numbers
  1.  int i =3;
  2. object o = i;//Implict boxing
  3. //This object is never unboxed
  4.  
Jan 8 '09 #1
3 1534
r035198x
13,262 MVP
No. Boxing is just computationally expensive that's all.
Jan 8 '09 #2
mldisibio
190 Recognized Expert New Member
Not memory leaks, but more memory is used when boxing value types:
Expand|Select|Wrap|Line Numbers
  1. // Value types are created on the stack.
  2. int i = 3; 
  3.  
  4. // A reference type (object) is created on the heap. 
  5. // "o" points to it.
  6. // The value copied to the heap is a copy of i, 
  7. // which is still on the stack
  8. object o = i; 
As opposed to reference types, where "boxing" is really just inheritance casting:

Expand|Select|Wrap|Line Numbers
  1. // Reference type created on the heap
  2. // "c" points to it
  3. MyClass c = new MyClass(); 
  4. // "o" is now a pointer to the same address of "c" on the heap
  5. object o = c; 
Jan 8 '09 #3
vekipeki
229 Recognized Expert New Member
When you say "This object is never unboxed", note that "boxing" a value type does not do anything to the original type, just like "unboxing" does not change the original reference type.

The name "boxing" is a bit confusing because it sounds like you are encapsulating your original value in some way, but you are actually creating a new object which has the initial value same as your original value.

Expand|Select|Wrap|Line Numbers
  1. int i = 1;
  2. object o = i;
  3.  
  4. // changing i will not change o
  5. i = 2;
  6.  
  7.  // this will display "1", not "2"
  8. Console.WriteLine(o);
Since your new object is a reference type, GC will collect it when it gets out scope, just as any other object, so there will not be any memory leaks.
Jan 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

43
6844
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
3
1789
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
4
5679
by: ali.jan | last post by:
Hi, It is trivial to load an assembly in a new Application Domain. Is there any way of loading an assembly in a new process? I tried using the Process class like this: Process p = new...
5
1743
by: Joe | last post by:
Consider the following code loop: for(int x = 0; x < 100; x++) { string sLoop = "Loop # " + (x+1).ToString(); Console.WriteLine(x); } I was told that the (x+1).ToString() was a boxing...
5
1610
by: cnickl | last post by:
Some time ago I was looking for a cheap or free way to check an application written in C# and some unsafe code for memory leaks. Someone in this forum suggested DevPartner form Compuware, telling...
0
1413
by: Antonio Arauzo Azofra | last post by:
Hello everybody, Probably, this is being too demanding for Python, but it may be useful to unimport modules to work with dynamic code (though not the best, one example is ). In fact, it is...
9
1605
by: Ryan Liu | last post by:
Hi, I use C# wrote an Client/Server application. In production environment, will be 130 clients (Windows XP) connect to a Server (Windows 2000/2003 Server) thought TCP/IP socket in a local 100M...
6
5553
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this...
16
5074
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.