memory - Why Initializing object to null is not dereferenced the object in java? -
i working application have implemented observer pattern set slider value on change in model. have button called clear clear window , set observer null before removing listener .so after did memory mapping profiler .but scared find object still there. in 2nd exp before making observer null first remove listener make null , found profiler object collected garbage collector. below provide snipnet of code .any light on appreciated .
controls = new createcontrols(); scene scene = new scene(new group()); stage.settitle("table view sample"); stage.setwidth(300); stage.setheight(500); final vbox vbox = new vbox(); vbox.setspacing(5); vbox.setpadding(new insets(10, 0, 0, 10)); controlbox = controls.creatcontrols(); vbox.getchildren().add(controlbox); final button clear = new button("clear"); vbox.getchildren().add(clear); eventhandler deletehnadler= new eventhandler<actionevent>(){ @override public void handle(actionevent arg0) { // todo auto-generated method stub vbox.getchildren().clear(); // controls.dispose(); controls=null; } };
createcontrol class:
package com.nxp.designpattern.observer; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.scene.control.button; import javafx.scene.control.slider; import javafx.scene.layout.vbox; public class createcontrols implements observerinterface { private slider slider; private static int counter; private subject m_subject; private button next_button; private button previous_buttbutton; private eventhandler<actionevent> nexteventhandler; private eventhandler<actionevent> prevhandler; public createcontrols() { m_subject = new subject(); observerimpl.getobserverimpl().addlistener(this); } public vbox creatcontrols() { final vbox vbox = new vbox(); vbox.setspacing(5); vbox.setpadding(new insets(10, 0, 0, 10)); next_button = new button("next"); previous_buttbutton = new button("previous"); slider = new slider(); vbox.getchildren().add(next_button); vbox.getchildren().add(previous_buttbutton); vbox.getchildren().add(slider); nexteventhandler = new eventhandler<actionevent>() { @override public void handle(actionevent arg0) { // todo auto-generated method stub counter += 10; m_subject.setvalue(counter); } }; next_button.addeventhandler(actionevent.action, nexteventhandler); prevhandler = new eventhandler<actionevent>() { @override public void handle(actionevent arg0) { // todo auto-generated method stub counter -= 10; m_subject.setvalue(counter); } }; previous_buttbutton.addeventhandler(actionevent.action, prevhandler); return vbox; } @override public void changeinvalue(int value) { slider.setvalue(value); } void dispose() { next_button.removeeventhandler(actionevent.action, nexteventhandler); // nexteventhandler=null; previous_buttbutton.removeeventhandler(actionevent.action, prevhandler); // prevhandler=null; observerimpl.getobserverimpl().removelistener(this); } }
"in 2nd exp before making observer null first remove listener make null"
problem/solution. variable pointer, setting null doesn't actual object , listener still has reference it'll never garbage collected. have remove references.
Comments
Post a Comment