473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boxing and UnBoxing

Joe
Consider the following code loop:

for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(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
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Apr 3 '06 #1
5 1750
Told by who?
I don't believe this, I don't think the garbage collector knows(or cares for
that matter) about boxing and unboxing.

"Joe" <js************ *@logicor.com> wrote in message
news:op******** *******@jsheble-laptop.logicorp hx2.com...
Consider the following code loop:

for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(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
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Apr 3 '06 #2
"Gabriel Lacatus" <cy*******@nosp am.nospam> wrote in
news:Oj******** ******@TK2MSFTN GP11.phx.gbl:
Told by who?
I don't believe this, I don't think the garbage collector knows(or
cares for that matter) about boxing and unboxing.


I agree... I'm not 100% sure, but I think the OP's issue here is that an
object will be created for the (x+1) in order to do the ToString. So while
this isn't a memory leak in the true sense of the word, if you were to do
something like this in a tight loop then it could cause memory exhaustion
and the GC to run more frequently, resulting in poor(er) performance.

-mdb
Apr 3 '06 #3
No boxing/unboxing occures.
Compiler is smart enough to eliminate the sequence into a Int32::ToString ()
directly.

Laura.

For curiosity, this is IL listing of your code snippet:

// Code size 42 (0x2a)
.maxstack 3
.locals init ([0] int32 x,
[1] int32 CS$0$0000)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: br.s IL_0024
IL_0004: ldstr "Loop # "
IL_0009: ldloc.0
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: stloc.1
IL_000d: ldloca.s CS$0$0000
IL_000f: call instance string [mscorlib]System.Int32::T oString()
IL_0014: call string [mscorlib]System.String:: Concat(string,
string)
IL_0019: pop
IL_001a: ldloc.0
IL_001b: call void [mscorlib]System.Console: :WriteLine(int3 2)
IL_0020: ldloc.0
IL_0021: ldc.i4.1
IL_0022: add
IL_0023: stloc.0
IL_0024: ldloc.0
IL_0025: ldc.i4.s 100
IL_0027: blt.s IL_0004
IL_0029: ret
"Joe" <js************ *@logicor.com> ha scritto nel messaggio
news:op******** *******@jsheble-laptop.logicorp hx2.com...
Consider the following code loop:

for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(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
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Apr 3 '06 #4
Thumbs up for the framework ! ;)

"Laura T." <laura_t@yahood otdotcom> wrote in message
news:Of******** ******@tk2msftn gp13.phx.gbl...
No boxing/unboxing occures.
Compiler is smart enough to eliminate the sequence into a
Int32::ToString () directly.

Laura.

For curiosity, this is IL listing of your code snippet:

// Code size 42 (0x2a)
.maxstack 3
.locals init ([0] int32 x,
[1] int32 CS$0$0000)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: br.s IL_0024
IL_0004: ldstr "Loop # "
IL_0009: ldloc.0
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: stloc.1
IL_000d: ldloca.s CS$0$0000
IL_000f: call instance string [mscorlib]System.Int32::T oString()
IL_0014: call string [mscorlib]System.String:: Concat(string,
string)
IL_0019: pop
IL_001a: ldloc.0
IL_001b: call void [mscorlib]System.Console: :WriteLine(int3 2)
IL_0020: ldloc.0
IL_0021: ldc.i4.1
IL_0022: add
IL_0023: stloc.0
IL_0024: ldloc.0
IL_0025: ldc.i4.s 100
IL_0027: blt.s IL_0004
IL_0029: ret
"Joe" <js************ *@logicor.com> ha scritto nel messaggio
news:op******** *******@jsheble-laptop.logicorp hx2.com...
Consider the following code loop:

for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(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
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Apr 3 '06 #5
Even though value types don't have object pointers (are not reference
types), methods such as Equals(), GetHashCode() and ToString() (virtual
methods) can be called as non virtual methods (no boxing required)

This is because Syste.ValueType overrides this methods and since you cannot
inherit from a value type the CLR can safely call this methods as non
virtual (no boxing)

Not all ValueType methods behave the same, methods such as GetType() or
MemberwiseClone () will require your value type to be boxed because this
methods are defined directly in the System.Object and (not automatically
overridden).

Other reasons why ValueTypes can be boxed is if you implement an interface
on the ValueType and then cast the ValueType to the interface, this will
cause the ValueType to be boxed.

Concerning the memory leak issue: When a ValueType gets boxed, it will
eventually become unreachable and then the garbage collector will clean it
up so there is no such thing as a memory leak.
"Joe" <js************ *@logicor.com> wrote in message
news:op******** *******@jsheble-laptop.logicorp hx2.com...
Consider the following code loop:

for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(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
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Apr 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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; }
24
2598
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
4
2962
by: Peter Olcott | last post by:
I want to be able to make my .NET applications run just as fast as unmanaged C++. From my currently somewhat limited understanding of the .NET framework and the C# language, it seems that Boxing/Unboxing might present of problem. Since C++ has pointer syntax, I was thinking that this might eliminate the need for Boxing and Unboxing. Am I...
94
5646
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
19
13699
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
7770
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every...
0
7923
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
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7974
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
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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
5719
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
3845
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
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.