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]

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 }
what does split do?thanks
oops... i'm messing it...the bracket i bracket does not appear...it should be c = b bracket i bracket ;
> what does split do?> thankssplit acts like a StringTokenizer.
> 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 >

//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
then use stringtokenizer :)
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
>
> 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 }
instead you could use [code] char c= b[</code]>
> then use stringtokenizer :)Sorry but that is not the issue...using either String.split or stringtokenizer will work fine.
da my stupidity...i advise people to use code tags and i cant use it...anyways you could use char c= b[i]
i did not see split function at http://java.sun.com/j2se/1.3/docs/api/java/lang/String.htmlok, good to know...thanks
> 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.
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
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.
> then use stringtokenizer :)which has a statement in the documentation saying explicitly it should NOT be used.Effectively the entire class is deprecated.
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.
> 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].
> 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 >
