Memory Leak

Will there be a memory leak on line 6? If no, when can it be possible to encouter memory leak?

Please advise. Im kinda confused. Thanks :)

Note: This is just a sample code

1 ArrayList x = new ArrayList();

2 String a = "a,b,,d,,,g,h,i,,k";

3 String [] b = a.split(",");

4

5 for (int i = 0; i < b.length; i++) {

6 String c = b;

7 x.add(c);

8 // do i need to put c= null here?

9 }

[461 byte] By [kneelmara] at [2007-11-27 4:29:59]
# 1

oops correction, line 6

it should be c = b

> Will there be a memory leak on line 6? If no, when

> can it be possible to encouter memory leak?

>

> Please advise. Im kinda confused. Thanks :)

>

> Note: This is just a sample code

>

> 1 ArrayList x = new ArrayList();

> 2 String a = "a,b,,d,,,g,h,i,,k";

> 3 String [] b = a.split(",");

> 4

> 5 for (int i = 0; i < b.length; i++) {

> 6 String c = b;

> 7 x.add(c);

> 8 // do i need to put c= null here?

> 9 }

kneelmara at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 2
what does split do?thanks
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 3
pls use code tags
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 4
oops... i'm messing it...the bracket i bracket does not appear...it should be c = b bracket i bracket ;
kneelmara at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 5
> what does split do?> thankssplit acts like a StringTokenizer.
kneelmara at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 6
> oops... i'm messing it...the bracket i bracket does> not appear...> > it should be c = b bracket i bracket ;Use code tags. See http://forum.java.sun.com/help.jspa?sec=formatting
hiwaa at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 7

//I think your c string will be marked for garbage collection at the end

// of the for cycle.

// As well as the temporary contents at each reassignment c=b[i]

BTW, you could use code tags to avoid [i] being converted to an Italic tag

sztyopeka at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 8
then use stringtokenizer :)
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 9

memory is managed by the garbage collector

String c = b;

should be

String c = b[i];

or simply

x.add(b[i]);

fyi: c's memory is only in use until c is out of scope (no longer referenced by anything) at the end of the loop, then it is available for garbage collection

memory leaks can occur in java when the garbage collector is not able to reclaim memory, e.g. static data, array reference, thread's that haven't been started

developer_jbsa at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 10

>

> 1 ArrayList x = new ArrayList();

> 2 String a = "a,b,,d,,,g,h,i,,k";

> 3 String [] b = a.split(",");

> 4

> 5 for (int i = 0; i < b.length; i++) {

> 6 String c = b;

> 7 x.add(c);

> 8 // do i need to put c= null here?

> 9 }

test using code tag

1 ArrayList x = new ArrayList();

2 String a = "a,b,,d,,,g,h,i,,k";

3 String [] b = a.split(",");

4

5 for (int i = 0; i < b.length; i++) {

6 String c = b[i];

7 x.add(c);

8 // do i need to put c= null here?

9 }

kneelmara at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 11
instead you could use [code] char c= b[</code]>
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 12
> then use stringtokenizer :)Sorry but that is not the issue...using either String.split or stringtokenizer will work fine.
kneelmara at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 13
da my stupidity...i advise people to use code tags and i cant use it...anyways you could use char c= b[i]
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 14
i did not see split function at http://java.sun.com/j2se/1.3/docs/api/java/lang/String.htmlok, good to know...thanks
geomana at 2007-7-12 9:39:09 > top of Java-index,Java Essentials,Java Programming...
# 15

> memory is managed by the garbage collector

>

> String c = b;

> should be

> String c = b[i];

> or simply

> x.add(b[i]);

> fyi: c's memory is only in use until c is out of

> scope (no longer referenced by anything) at the end

> of the loop, then it is available for garbage

> collection

> memory leaks can occur in java when the garbage

> collector is not able to reclaim memory, e.g. static

> data, array reference, thread's that haven't been

> started

can please give me a sample code that has a memory leak?

e.g. static

> data, array reference, thread's that haven't been

> started

Thanks.

kneelmara at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 16

try these for OutOfMemoryError, you may have to adjust the looping or size

for( int i=0; i<100000; i++ ) {

new Thread(); // .start(); will allow gc and no memory leak

}

or

new Object[100000];

oversimplified, but in a complicated program multiple references to arrays or large amounts of static data (not usually no-start Threads) can cause OutOfMemoryError

these are not necessarily leaks but mismanaged memory

Message was edited by:

developer_jbs

developer_jbsa at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 17

Hi

Your code is a typical (common) one.

I don't see any memory leak at all.

Note that after every loop, your variable String c

will just refer to an already existing String object that have been previously created by the String.split()

invocation.

So if you will analyze, you're no longer creating a brand new object because you're just referencing an already existing object in the heap. And that your String c is just a pointer to an object and nothing much - period.

What's the effect of this to the memory? You can store as much as you can to the ArrayList object - and your memory is the limit. You cannot say that it is a memory leak - period.

eadelarosaa at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 18
> then use stringtokenizer :)which has a statement in the documentation saying explicitly it should NOT be used.Effectively the entire class is deprecated.
jwentinga at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 19
its not deprecated...though it is recommended to use String.split() method or the package util.regex (am not sure if that's the correct package)...StringTokenizer is considered as a legacy class.
kneelmara at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 20

> Will there be a memory leak on line 6?

No.

> If no, when

> can it be possible to encouter memory leak?

Frankly, you'd be better off searching the Web for some article that will give you much more details.[url=http://www-128.ibm.com/developerworks/java/library/j-leaks/] Here's one[/url].

karma-9a at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...
# 21

> i did not see split function at

> http://java.sun.com/j2se/1.3/docs/api/java/lang/String

> .html

>

> ok, good to know...

> thanks

Started in version 1.4:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

jbisha at 2007-7-21 21:07:41 > top of Java-index,Java Essentials,Java Programming...