Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 garbage collection in C#
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mirhagk




PostPosted: Sat Nov 06, 2010 12:24 am   Post subject: garbage collection in C#

okay I know this has been answered a million times before, but say for instance I have an array of 10000 elements, but it's in a lisp-style list (where the array is simply a pointer to the first element which has a value and the pointer to the second object which has value and ptr to 3rd etc) whats happening in my code is that it adds new elements to the end very often, removing them from the list, only it doesnt. It basically sets the previous element to equal the next one, but the current element still exits. How do I make it not exist? (its causing a stack overflow unless array is like 1000)

I even tried keeping track of the "deletion" of objects and after every 1000 to call the garbage collecter, but it still causes stack overflow. How do I properly flag something for delete?

it crashes consistently after about 16000 allocations of objects
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Sat Nov 06, 2010 7:39 am   Post subject: Re: garbage collection in C#

You really can't tell the garbage collector what to do. If you call it, it'll still run whenever it damn well pleases. It can also be a bad thing to call the GC too often manually, as objects will appear more important to the GC and may stick around longer.

What kind of array are you using? Just your standard array? You may want to look into read up on C# memory management and you may want also want to look into stackalloc arrays.
mirhagk




PostPosted: Sat Nov 06, 2010 9:35 am   Post subject: RE:garbage collection in C#

no im not using standard arrays, the first element has a pointer to the second, which has a pointer to the third, which has a pointer to the fourth. It's non-lnear and kinda all over the place.

The problem is that I can't seem to tell the GC that I don't need elements of the array anymore.

Edit: found the problem, the "array" calls sort recursively on itself, and as its constantly expanding the stack overflows from method calls. Fixed now by having a static method simply go through them all. I'm going to look at a few optimizations, but for the most part it works (also had an issue where the beginning of the array appeared unsorted by I discovered that since the "array" was a pointer to the first element, when the first element moved to the end, the pointer was left looking at dead space.
2goto1




PostPosted: Sat Nov 06, 2010 10:04 am   Post subject: RE:garbage collection in C#

Are you sure that it's a memory issue? You mentioned that stack overflows are occurring...when do they occur? It sounds like you've created a custom programmed singly linked list? Is it a FIFO (first in, first out) type of queue that you've created?

Stack overflow exceptions usually occur when there is too much recursion going on that the .NET runtime no longer has enough memory to allocate on the stack for future recursive method invocations. If that is the case then the issue may not be a matter of garbage collection, but may be a matter of algorithm implementation.
rdrake




PostPosted: Sat Nov 06, 2010 2:46 pm   Post subject: Re: RE:garbage collection in C#

2goto1 @ Sat Nov 06, 2010 10:04 am wrote:
Is it a FIFO (first in, first out) type of queue that you've created?
Agreed. .NET has this wonderful Queue class for just such an occasion.
mirhagk




PostPosted: Sat Nov 06, 2010 8:42 pm   Post subject: RE:garbage collection in C#

no, not a Fifo kind of thing, just a single object that has a value and a link to another object. A binary tree, lisp-list style. You have indirect access to every node, and direct access to the first (and the last in my program)
2goto1




PostPosted: Sat Nov 06, 2010 9:05 pm   Post subject: RE:garbage collection in C#

is your node traversal recursive?
mirhagk




PostPosted: Sat Nov 06, 2010 9:44 pm   Post subject: RE:garbage collection in C#

yeah it kinda has to be, theres no direct link to any element but the first. Its made for dynamically adding and deleting and moving. I wanna implement a very fast sort with it. I have a better idea than the one above.

but basically it never requires a resizing and allows adding and deleting things from the middle of the array, so its useful in some scenarios

edit: So basically i made a linked list.. lol
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: