473,587 Members | 2,501 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 1541
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
6859
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
3
1791
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
4
5696
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 Process() p.StartInfo.FileName = mStartupFile p.StartInfo.UseShellExecute = False
5
1750
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 statement, and would therefore cause a memory leak because there's no explicit unboxing
5
1620
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 me that there is a free basic version out there. However I can’t find it. Nowhere on the Compuware website is a link to anything under $4000. Am I...
0
1415
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 supposed to be possible, but I have detected it usually leaks memory. When unimported in Linux, the simple C module attached (has no functions, just...
9
1609
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 LAN. "Server" is running as a Windows service. The is one thread running for one clinet in the server. Sometime the user tells me the Sever will...
6
5562
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 particular crash so I don't think anyone else is experiencing it but I know other users are doing exactly what she is doing because it is our most...
16
5085
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 the ability to allocate different size chunks of memory not just a single size. It should error check for double free, etc. And it should be usable...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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 we have to send another system
0
1189
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.