473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

unsafe operations

if I have an unsafe method that takes in two arrays, performs some
operations and returns; I know that the garbage collector will not
collect objects allocated inside the unsafe method. However, if I'm
calling this method from a main() method of a class which is safe (i.e
managed), will the allocated array (which, suppose, has been allocated
inside the main() method) be collected by the garbage collector after
it's been used? Also, how do we delete heap allocated objects (suppose a
temporary array) from within the unsafe method? Is the unsafe method
treated as a region (term taken from strict Realitime environment) and
discarded by the GC once it's been used (or is it the method stack
that's discarded)? Just making sure I know what my code does before I
implement it :) thanks

-Andre

Nov 15 '05 #1
3 3134
The GC will collect objects in an unsafe block. The trick is to pin the
objects so the GC *doesn't* collect them while you're using them. That's
what the "fixed" block is for. Look up the Fixed block in the help file, and
there's a sample. Once the code gets passed the fixed block, the objects
become unpinned, and the GC is able to collect them on the next round.

-Rob [MVP]

"Andre" <fo********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
if I have an unsafe method that takes in two arrays, performs some
operations and returns; I know that the garbage collector will not
collect objects allocated inside the unsafe method. However, if I'm
calling this method from a main() method of a class which is safe (i.e
managed), will the allocated array (which, suppose, has been allocated
inside the main() method) be collected by the garbage collector after
it's been used? Also, how do we delete heap allocated objects (suppose a
temporary array) from within the unsafe method? Is the unsafe method
treated as a region (term taken from strict Realitime environment) and
discarded by the GC once it's been used (or is it the method stack
that's discarded)? Just making sure I know what my code does before I
implement it :) thanks

-Andre

Nov 15 '05 #2
Thanks Rob, that answers my question.
cheers,

-Andre

Rob Teixeira [MVP] wrote:
The GC will collect objects in an unsafe block. The trick is to pin the
objects so the GC *doesn't* collect them while you're using them. That's
what the "fixed" block is for. Look up the Fixed block in the help file, and
there's a sample. Once the code gets passed the fixed block, the objects
become unpinned, and the GC is able to collect them on the next round.

-Rob [MVP]

"Andre" <fo********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
if I have an unsafe method that takes in two arrays, performs some
operations and returns; I know that the garbage collector will not
collect objects allocated inside the unsafe method. However, if I'm
calling this method from a main() method of a class which is safe (i.e
managed), will the allocated array (which, suppose, has been allocated
inside the main() method) be collected by the garbage collector after
it's been used? Also, how do we delete heap allocated objects (suppose a
temporary array) from within the unsafe method? Is the unsafe method
treated as a region (term taken from strict Realitime environment) and
discarded by the GC once it's been used (or is it the method stack
that's discarded)? Just making sure I know what my code does before I
implement it :) thanks

-Andre



Nov 15 '05 #3
I'd have to re-read the specs on the current GC, but the bottom line is that
at some point, everything gets collected.
There is no explicit delete of managed memory.

-Rob

"Andre" <fo********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
An afterthought - how do I explicitly 'delete' an object from within an
unsafe block like in C++? Is there an analogous operation available for
that? thanks

-Andre

Andre wrote:
One more thing... what if the unsafe code block allocated space larger
than 85Kb (i.e a single object was more than 85Kb), this would mean that
the GC would store this in the Large Object Space, which isn't
collected, and we'll end up with no or little memory? Or is this not the
case with unsafe code blocks?

-Andre
Rob Teixeira [MVP] wrote:
The GC will collect objects in an unsafe block. The trick is to pin the
objects so the GC *doesn't* collect them while you're using them. That's what the "fixed" block is for. Look up the Fixed block in the help
file, and
there's a sample. Once the code gets passed the fixed block, the objects become unpinned, and the GC is able to collect them on the next round.

-Rob [MVP]

"Andre" <fo********@hotmail.com> wrote in message
news:3F**************@hotmail.com...

if I have an unsafe method that takes in two arrays, performs some
operations and returns; I know that the garbage collector will not
collect objects allocated inside the unsafe method. However, if I'm
calling this method from a main() method of a class which is safe (i.e
managed), will the allocated array (which, suppose, has been allocated
inside the main() method) be collected by the garbage collector after
it's been used? Also, how do we delete heap allocated objects (suppose a temporary array) from within the unsafe method? Is the unsafe method
treated as a region (term taken from strict Realitime environment) and
discarded by the GC once it's been used (or is it the method stack
that's discarded)? Just making sure I know what my code does before I
implement it :) thanks

-Andre

Nov 15 '05 #4

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

Similar topics

2
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged?...
4
by: Jon Milner | last post by:
How do I declare that my code is unsafe? Sorry the help files at my University have not been installed!
17
by: Bradley1234 | last post by:
Sorry if this is obvious, but Ill ask... Is there a new way of using pointer operations in C# ? Ive got a Deitel book on C# that neither mentions the word "pointer" nor "unsafe" in the index....
7
by: TomHL | last post by:
hello, - I have a Bitmap Object named: bmp1 - I already have the numerical values of:red,blue and green colors + I have the X and Y cordinates. How do I set the pixel value via safe code on...
12
by: JS | last post by:
I was writing some routines which could do bitwise boolean operations on byte arrays, and I ran into what I think is a bug with C#'s unsafe code. I am pasting a console application below. Can...
10
by: Sergei | last post by:
Can anyone explain why PostMessage to a Windows Form is considered unsafe and raises InvalidOperationException? I can get rid of that setting CheckForIllegalCrossThreadCalls to false and...
1
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to write a Stored Procedure CLR for SQL Server 2005 to get some disk information. I have this code working although the assemble has to be create with "PERMISSION_SET = UNSAFE". Why do...
8
by: pjerald | last post by:
package test; import java.util.ArrayList; public class MyArrayList{ public static void main(String args) { ArrayList al = new ArrayList(); al.add("Jerald"); ...
0
by: colin | last post by:
Hi, I have a binary sorted tree wich I find my app is spending most of its time in so I decided to try speed it up a bit ... I tested converting it from classes to an indexed array of structs so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...

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.