We were given the following code sample at university and we were told that there would be a memory leak while running this code is . The sample should show that this is a situation where garbage collector can not work.
Say, the elements are copied. Does this mean that the context has been copied (and so another entry is made on the heap) or the object is being copied itself? As far as I know, objects and therefore objects [] are implemented as a reference type. Therefore, by specifying a new value for 'items', the garbage collector will have to find out that the old 'item' is no longer referred and hence it can be collected. In my eyes, this codeample does not generate memory leak. Can someone prove me wrong? =) Item = Arrays.copyOf (item, 2 * size + 1) As far as my object-oriented programming goes, only the codeline capable of creating memory leakage
;
Import java.util.Arrays; Public square fu {private object [] items; Private integer size = 0; Private static final ISIEG = 10; Public Fu () {items = new object [ISIZE]; } Public Zero Push (Last Object o) {checkSize (); Items [size ++] = o; } Public Object Pop () {If (size == 0) take new ... / / ... back item [- size]; } Private Zero check (size) {if (items.length == size) {items = Arrays.copyOf (item, 2 * size + 1); }}}}
sign: leakage pop Method consider what happens to the contexts of a POPED object ...
Comments
Post a Comment