ArrayList memory problem

P

Peter Schmitz

Hi everyone,

I've got the following problem: In my application I make use of two
ArrayList objects like this:

Public Sub Main()
LoadData()
End Sub

Public Function LoadData()
Dim list1 as new ArrayList()
[...Fill the arraylist...]
AnalyzeData(list1)
End Function

Public Function AnalyzeData(list1 as ArrayList)
Dim list2 as new ArrayList
list2 = list1
[...do stuff...]
list2.Clear()
End Function

Now, this code creates a major memory leak....calling GC.Collect() etc.
won't help. Can anyone tell me what happens, when I call list2 = list1 - is
the whole list copied, or just a pointer??
I know this is a stupid program structure, but for some reasons I have to
stick to it...:/

Greetings,

Peter
 
P

PvdG42

Peter Schmitz said:
Hi everyone,

I've got the following problem: In my application I make use of two
ArrayList objects like this:

Public Sub Main()
LoadData()
End Sub

Public Function LoadData()
Dim list1 as new ArrayList()
[...Fill the arraylist...]
AnalyzeData(list1)
End Function

Public Function AnalyzeData(list1 as ArrayList)
Dim list2 as new ArrayList
list2 = list1
[...do stuff...]
list2.Clear()
End Function

Now, this code creates a major memory leak....calling GC.Collect() etc.
won't help. Can anyone tell me what happens, when I call list2 = list1 -
is
the whole list copied, or just a pointer??
I know this is a stupid program structure, but for some reasons I have to
stick to it...:/

Greetings,

Peter

Now that you know from the answers and demo code you received from your
first post of this question, that the statement "list2 = list1" simply
assigns the address of the first ArrayList to the object variable list2,
leaving you no active reference to the second ArrayList created with the
statement "Dim list2 as new ArrayList", what memory leak are you talking
about?
 
H

Herfried K. Wagner [MVP]

Peter --

Am 21.01.2010 20:16, schrieb Peter Schmitz:
I've got the following problem: In my application I make use of two
ArrayList objects like this:

Public Sub Main()
LoadData()
End Sub

Public Function LoadData()
Dim list1 as new ArrayList()
[...Fill the arraylist...]
AnalyzeData(list1)
End Function

Public Function AnalyzeData(list1 as ArrayList)
Dim list2 as new ArrayList
list2 = list1
[...do stuff...]
list2.Clear()
End Function

Now, this code creates a major memory leak....calling GC.Collect() etc.
won't help. Can anyone tell me what happens, when I call list2 = list1 - is
the whole list copied, or just a pointer??

An additional reference to the object referenced by the named reference
(variable) 'list1' will be created. No objects will be created or copied.
I know this is a stupid program structure, but for some reasons I have to
stick to it...:/

I am wondering why you have to stick to this program structure.

There is no reason to declare 'Dim list2 As New ArrayList()', which will
create an additional 'ArrayList' object that is never used because the
statement 'list2 = list1' will make it unreachable for code and thus the
GC will destroy it sooner or later.

What exactly are you attempting to achieve? Maybe we'll find a better
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top