How to prove that a destructor must be defined for the Value classes

4 次查看(过去 30 天)
The book tell me that Handle classes has build-in Destructor function.
On the other way Vlaue classes need user build Destructor function,otherwise, MATLAB will not be able to release the memory of the Value object
Matlab will release the handle object‘s memory automaticlly when the function close
For example,MatrixHandle is a handle classes,function foo close,matlab will release hlmatrix’s memory automaticlly。
clear all ; clc ;
foo(10000);
function foo(N,tracker)
hlmatrix = MatrixHandle(N);
end
So,if there is a Value classes RMB ,user didn't build a Destructor function. Matlab cann't releaser the Value object's memory automaticlly? So I did an experiment!
classdef RMB % Value classes
properties(SetAccess = private)
amount
end
methods % has no Destructor function
function obj = RMB(val)
obj.amount = val;
end
function newobj = times(obj,multiplier)
newobj = RMB(obj.amount*multiplier) ;
end
end
end
use a Value object in a function,I expect that after the program runs, the memory of k.amount has not been released.Because I didn't build a Destructor function for Value classes! After the program ran, k.amount disappeared from the workspace.This is weird! Why is it automatically cleared by MATLAB?How can I prove that Value classes require users to build a Destructor function, and users must manually use the Destructor function ‘delete’ to release memory,Otherwise, the Value object‘s memory will be permanently occupied!
function Y=testvecgtn(varargin)
%UNTITLED
tic
k=RMB(varargin{1});%creat a handle object
Y=k.amount;
toc
end
z=testvecgtn(100)

采纳的回答

Catalytic
Catalytic 2023-8-2
编辑:Catalytic 2023-8-2
I think there is an argument to be made for allowing destructors to be defined by value classes, but not for memory management purposes. For example, in this class, we open a file upon construction, and might want the file automatically closed upon destruction -
classdef valueClass
properties
fid
prop1,prop2,prop3
end
methods
function obj=valueClass(filename)
obj.fid=fopen(filename);
end
function delete(obj)
fclose(obj.fid);
end
end
end
Obviously, the delete method will have to be invoked manually here if I want the fid closed, because I have chosen not to inherit the class from handle.
Some people would ask "well why not just make it a handle class?" to which my response would be "well why should I have to?"
  7 个评论
fa wu
fa wu 2023-8-3
Thanks for your comment.
“ I don't think that's the behavior you want.”--------I don't get the point.User use "fclose(obj.fid)" in customized destructors ,that allows matlab close file automatically when the function exits ,Isn't that good?
open file----->read data---->close file---->release memory .Isn't this our purpose?

请先登录,再进行评论。

更多回答(2 个)

Steven Lord
Steven Lord 2023-8-1
The book tell me that Handle classes has build-in Destructor function.
Which book is that? Are you referring to the documentation? If so which page are you referencing here?
On the other way Vlaue classes need user build Destructor function,otherwise, MATLAB will not be able to release the memory of the Value object
Incorrect. When there are no longer any references to a variable containing an instance of a value class, it gets destroyed. The most common scenario for this is probably a function where the variable is created completing its execution. See "Destructor methods" in the "Kinds of Methods" section on this documentation page.
So,if there is a Value classes RMB ,user didn't build a Destructor function. Matlab cann't releaser the Value object's memory automaticlly?
Incorrect. If that were the case, how would memory for a double precision array (which uses value semantics) get released? delete isn't defined for double arrays.
which -all delete
built-in (/MATLAB/toolbox/matlab/io/filesystem/delete) delete is a built-in method % meta.PackageList method delete is a built-in method % handle method delete is a built-in method % meta.ClassList method delete is a built-in method % meta.FunctionList method delete is a built-in method % meta.TypeList method delete is a built-in method % meta.EnumeratedValue method delete is a built-in method % meta.MetaData method /MATLAB/toolbox/matlab/iofun/@timer/timer.m % timer method /MATLAB/toolbox/matlab/randfun/@RandStream/RandStream.m % RandStream method /MATLAB/toolbox/matlab/connector2/worker/+connector/+internal/Worker.p % connector.internal.Worker method delete is a built-in method % connector.internal.Logger method delete is a built-in method % meta.package method delete is a built-in method % meta.method method delete is a built-in method % inputParser method delete is a built-in method % matlab.mixin.Copyable method /MATLAB/toolbox/matlab/connector2/messageservice/+message/+internal/Subscription.p % message.internal.Subscription method delete is a built-in method % meta.class method delete is a built-in method % matlab.ui.internal.mixin.Printable method delete is a built-in method % matlab.ui.internal.mixin.Windowable method delete is a built-in method % matlab.ui.eventdata.WindowCloseRequestData method delete is a built-in method % matlab.ui.Figure method delete is a built-in method % matlab.ui.internal.mixin.AutoResizable method delete is a built-in method % matlab.ui.internal.mixin.AxesLimits method delete is a built-in method % matlab.ui.internal.mixin.Legacy method delete is a built-in method % matlab.ui.internal.mixin.FontRenderable method delete is a built-in method % matlab.ui.internal.mixin.KeyInvokable method delete is a built-in method % matlab.ui.internal.mixin.ComponentLayoutable method delete is a built-in method % matlab.ui.internal.mixin.NoMarginsUnitsOnlyPositionable method delete is a built-in method % matlab.ui.internal.mixin.Positionable method delete is a built-in method % matlab.ui.internal.mixin.OuterPositionable method delete is a built-in method % matlab.ui.internal.mixin.UIOuterPositionable method delete is a built-in method % matlab.ui.internal.mixin.ReadOnlyPositionable method delete is a built-in method % matlab.ui.internal.mixin.Selectable method delete is a built-in method % matlab.ui.internal.mixin.Scrollable method delete is a built-in method % matlab.ui.internal.mixin.CanvasHostMixin method delete is a built-in method % matlab.ui.internal.mixin.TerminalStateRepresentable method delete is a built-in method % matlab.ui.Root method delete is a built-in method % matlab.ui.control.ClientComponent method delete is a built-in method % matlab.ui.control.Component method delete is a built-in method % matlab.ui.container.Container method delete is a built-in method % matlab.ui.container.CanvasContainer method delete is a built-in method % matlab.ui.control.WebComponent method delete is a built-in method % matlab.ui.container.ButtonGroup method delete is a built-in method % matlab.ui.container.internal.UIContainer method delete is a built-in method % matlab.ui.container.Panel method delete is a built-in method % matlab.ui.container.Tab method delete is a built-in method % matlab.ui.container.TabGroup method delete is a built-in method % matlab.ui.control.UIControl method delete is a built-in method % matlab.ui.container.ContextMenu method delete is a built-in method % matlab.ui.container.Menu method delete is a built-in method % matlab.ui.control.Table method delete is a built-in method % matlab.ui.container.toolbar.PushTool method delete is a built-in method % matlab.ui.container.toolbar.ToggleTool method delete is a built-in method % matlab.ui.internal.mixin.UIToggleToolMixin method delete is a built-in method % matlab.ui.internal.mixin.UIToolMixin method delete is a built-in method % matlab.ui.container.Toolbar method delete is a built-in method % matlab.ui.internal.UnitsService method delete is a built-in method % matlab.ui.internal.UnitsServiceStorageInterface method delete is a built-in method % matlab.ui.internal.UnitsServiceStorage method delete is a built-in method % matlab.graphics.mixin.Focusable method delete is a built-in method % matlab.graphics.internal.GraphicsUIProperties method delete is a built-in method % matlab.graphics.internal.GraphicsCoreProperties method delete is a built-in method % matlab.graphics.mixin.GraphicsPickable method delete is a built-in method % matlab.graphics.mixin.Layoutable method delete is a built-in method % matlab.graphics.mixin.ViewPropertiesManager method delete is a built-in method % matlab.graphics.internal.Legacy method delete is a built-in method % matlab.graphics.mixin.Mixin method delete is a built-in method % matlab.graphics.mixin.NodeParent method delete is a built-in method % matlab.graphics.eventdata.ChildData method delete is a built-in method % matlab.graphics.mixin.NodeChildren method delete is a built-in method % matlab.graphics.mixin.Pickable method delete is a built-in method % matlab.graphics.mixin.Selectable method delete is a built-in method % matlab.graphics.mixin.Themeable method delete is a built-in method % matlab.graphics.mixin.ThemeContainer method delete is a built-in method % matlab.graphics.internal.GraphicsJavaVisible method delete is a built-in method % matlab.graphics.internal.GraphicsBaseFunctions method delete is a built-in method % matlab.graphics.Graphics method delete is a built-in method % matlab.graphics.GraphicsPlaceholder method delete is a built-in method % matlab.graphics.mixin.internal.Copyable method delete is a built-in method % matlab.graphics.internal.GraphicsPropertyHandler method delete is a built-in method % matlab.graphics.internal.ReferenceObject method delete is a built-in method % matlab.graphics.internal.GraphicsMetaProperty method delete is a built-in method % matlab.graphics.internal.GraphicsMetaEvent method delete is a built-in method % matlab.graphics.internal.GraphicsMetaClass method delete is a built-in method % matlab.graphics.internal.Exportable method delete is a built-in method % matlab.mixin.SetGet method delete is a built-in method % hgsetget method delete is a built-in method % dynamicprops method delete is a built-in method % meta.property method delete is a built-in method % meta.event method delete is a built-in method % JavaVisible method delete is a built-in method % matlab.mixin.internal.DefaultFactoryPropHandler method delete is a built-in method % matlab.internal.language.ir_workspace_ownership_proxy method delete is a built-in method % connector.internal.StoreGrootAppdata method delete is a built-in method % appdesigner.internal.application.AppDesignerServiceProvider method delete is a built-in method % appdesservices.internal.peermodel.BrowserControllerFactory method delete is a built-in method % appdesigner.internal.application.startup.StartupStateProviderFactory method delete is a built-in method % appdesigner.internal.application.figure.FigureService method delete is a built-in method % appdesigner.internal.application.startup.DesktopStartupStateProviderFactory method delete is a built-in method % appdesigner.internal.application.figure.DesktopFigureService method delete is a built-in method % viewmodel.internal.factory.ManagerFactoryProducer method delete is a built-in method % viewmodel.internal.factory.CPPVMMFactoryHelper method delete is a built-in method % viewmodel.internal.factory.MATLABVMMFactoryHelper method delete is a built-in method % appdesigner.internal.application.startup.OnlineStartupStateProviderFactory method delete is a built-in method % appdesigner.internal.application.figure.OnlineFigureService method /MATLAB/toolbox/matlab/general/onCleanup.m % onCleanup method delete is a built-in method % matlab.settings.SettingsGroup method delete is a built-in method % matlab.settings.Setting method delete is a built-in method % javahandle.com.mathworks.hg.peer.HG2FigurePeer method delete is a built-in method % matlab.graphics.eventdata.MouseData method delete is a built-in method % matlab.graphics.eventdata.Update method delete is a built-in method % matlab.graphics.axis.AbstractAxes method delete is a built-in method % matlab.graphics.axis.Axes method delete is a built-in method % matlab.graphics.axis.decorator.AxisDecoration method delete is a built-in method % matlab.graphics.axis.decorator.AxisRulerBase method delete is a built-in method % matlab.graphics.axis.decorator.Backdrop method delete is a built-in method % matlab.graphics.axis.decorator.BoxFrame method delete is a built-in method % matlab.graphics.axis.dataspace.CartesianDataSpace method delete is a built-in method % matlab.graphics.axis.colorspace.ColorSpace method delete is a built-in method % matlab.graphics.axis.dataspace.DataSpace method delete is a built-in method % matlab.graphics.axis.decorator.DecorationContainer method delete is a built-in method % matlab.graphics.axis.decorator.Grid method delete is a built-in method % matlab.graphics.axis.HintConsumer method delete is a built-in method % matlab.graphics.axis.colorspace.MapColorSpace method delete is a built-in method % matlab.graphics.axis.decorator.NumericRuler method delete is a built-in method % matlab.graphics.axis.PlotTarget method delete is a built-in method % matlab.graphics.axis.PlotTargetManager method delete is a built-in method % matlab.graphics.axis.decorator.Ruler method delete is a built-in method % matlab.graphics.axis.decorator.ScalableAxisRuler method delete is a built-in method % matlab.graphics.axis.decorator.SimpleGrid method delete is a built-in method % matlab.graphics.axis.dataspace.UniformCartesianDataSpace method delete is a built-in method % matlab.graphics.axis.camera.Camera method delete is a built-in method % matlab.graphics.axis.camera.Camera2D method delete is a built-in method % matlab.graphics.axis.camera.Camera3D method delete is a built-in method % matlab.graphics.primitive.Image method delete is a built-in method % matlab.graphics.primitive.Patch method delete is a built-in method % matlab.graphics.primitive.SurfaceXYZData method delete is a built-in method % matlab.graphics.primitive.Text method delete is a built-in method % matlab.graphics.interactor.ListOfPointsHighlight method delete is a built-in method % matlab.graphics.layout.TiledChartLayout method delete is a built-in method % matlab.graphics.layout.Layout method delete is a built-in method % matlab.graphics.layout.Text method delete is a built-in method % matlab.graphics.mixin.AbstractAxesParentable method delete is a built-in method % matlab.graphics.mixin.AffectAutoLimitsMixin method delete is a built-in method % matlab.graphics.mixin.AxesParentable method delete is a built-in method % matlab.graphics.mixin.Background method delete is a built-in method % matlab.graphics.mixin.ChartLayoutable method delete is a built-in method % matlab.graphics.eventdata.LegendableObjectsUpdated method delete is a built-in method % matlab.graphics.mixin.LegendTarget method delete is a built-in method % matlab.graphics.mixin.Legendable method delete is a built-in method % matlab.graphics.mixin.ColorOrderUser method delete is a built-in method % matlab.graphics.mixin.PolarAxesParentable method delete is a built-in method % matlab.graphics.mixin.GeographicAxesParentable method delete is a built-in method % matlab.graphics.mixin.MapAxesParentable method delete is a built-in method % matlab.graphics.mixin.SceneNodeGroup method delete is a built-in method % matlab.graphics.mixin.UIParentable method delete is a built-in method % matlab.graphics.primitive.world.BackdropPrimitive method delete is a built-in method % matlab.graphics.primitive.world.BoxPrimitive method delete is a built-in method % matlab.graphics.primitive.world.ClipNode method delete is a built-in method % matlab.graphics.primitive.world.Geometry method delete is a built-in method % matlab.graphics.primitive.world.GridPrimitive method delete is a built-in method % matlab.graphics.primitive.world.LightSource method delete is a built-in method % matlab.graphics.primitive.world.LineLoop method delete is a built-in method % matlab.graphics.primitive.world.LineStrip method delete is a built-in method % matlab.graphics.primitive.world.Marker method delete is a built-in method % matlab.graphics.primitive.world.PrimitiveBase method delete is a built-in method % matlab.graphics.primitive.world.PrimitiveContainer method delete is a built-in method % matlab.graphics.primitive.world.Quadrilateral method delete is a built-in method % matlab.graphics.primitive.world.RulerPrimitive method delete is a built-in method % matlab.graphics.primitive.world.Text method delete is a built-in method % matlab.graphics.primitive.world.TriangleStrip method delete is a built-in method % matlab.graphics.eventdata.Annotation method delete is a built-in method % matlab.graphics.eventdata.LegendEntry method delete is a built-in method % matlab.graphics.eventdata.UpdateState method delete is a built-in method % matlab.graphics.primitive.Data method delete is a built-in method % matlab.graphics.primitive.world.Group method delete is a built-in method % matlab.graphics.primitive.world.Positionable method delete is a built-in method % matlab.graphics.primitive.Group method delete is a built-in method % matlab.graphics.primitive.Transform method delete is a built-in method % matlab.graphics.eventdata.Reparent method delete is a built-in method % matlab.graphics.primitive.world.SceneNode method delete is a built-in method % matlab.graphics.eventdata.PostUpdate method delete is a built-in method % matlab.graphics.primitive.canvas.Canvas method delete is a built-in method % matlab.graphics.primitive.canvas.CanvasFactory method delete is a built-in method % matlab.graphics.interaction.interactioncontainers.CartesianAxesInteractionContainer method delete is a built-in method % matlab.graphics.interaction.interactioncontainers.StandardAxesInteractionContainer method delete is a built-in method % matlab.graphics.interaction.interactioncontainers.BaseAxesInteractionContainer method delete is a built-in method % matlab.graphics.interaction.interactioncontainers.InteractionContainer method delete is a built-in method % matlab.graphics.primitive.canvas.JavaCanvasFactory method delete is a built-in method % matlab.graphics.primitive.canvas.JavaCanvas method delete is a built-in method % matlab.graphics.internal.CanvasSetup method delete is a built-in method % matlab.graphics.shape.internal.ScribeStackManager method delete is a built-in method % matlab.graphics.chart.interaction.DataAnnotatable method delete is a built-in method % matlab.graphics.mixin.DataProperties method delete is a built-in method % matlab.graphics.chart.primitive.Line method delete is a built-in method % matlab.graphics.chart.mixin.SurfaceBrushable method delete is a built-in method % matlab.graphics.chart.interaction.SurfaceBrushing method /MATLAB/toolbox/matlab/scribe/+matlab/+graphics/+shape/+internal/@ScribeLayer/ScribeLayer.p % matlab.graphics.shape.internal.ScribeLayer method delete is a built-in method % matlab.graphics.shape.internal.AnnotationPane method delete is a built-in method % matlab.internal.language.localized_anonymous_function_handle_workspace_ownership_proxy method delete is a built-in method % event.listener method delete is a built-in method % matlab.graphics.controls.internal.ToolbarFactory method /MATLAB/toolbox/matlab/graphics/+matlab/+graphics/+controls/@ToolbarController/ToolbarController.p % matlab.graphics.controls.DesktopToolbarController method delete is a built-in method % matlab.graphics.controls.internal.ToolbarValidator method /MATLAB/toolbox/matlab/graphics/+matlab/+graphics/+controls/+internal/PointerMixin.p % matlab.graphics.controls.internal.PointerMixin method delete is a built-in method % matlab.graphics.controls.internal.FigureBasedModeStrategy method delete is a built-in method % matlab.graphics.controls.internal.AbstractModeStrategy method delete is a built-in method % matlab.graphics.controls.internal.FigurePointerModeStrategy method delete is a built-in method % matlab.graphics.controls.internal.PointerModeStrategy method delete is a built-in method % matlab.graphics.controls.internal.PostUpdatePlugin method delete is a built-in method % event.EventData method delete is a built-in method % matlab.graphics.eventdata.ChildEventData method delete is a built-in method % matlab.graphics.chart.primitive.Surface method delete is a built-in method % matlab.internal.language.nested_function_handle_ownership_proxy method delete is a built-in method % event.proplistener method delete is a built-in method % event.PropertyEvent method delete is a built-in method % matlab.internal.i18n.locale method delete is a built-in method % matlab.graphics.interaction.interactioncontainers.TextInteractionContainer method delete is a built-in method % matlab.graphics.interaction.internal.UnifiedAxesInteractions method delete is a built-in method % matlab.internal.editor.figure.FigureUtils method delete is a built-in method % meta.DynamicProperty method delete is a built-in method % matlab.graphics.interaction.uiaxes.EditInteraction method delete is a built-in method % matlab.graphics.interaction.internal.InteractionsList method delete is a built-in method % matlab.graphics.interaction.graphicscontrol.ControlManager method delete is a built-in method % matlab.graphics.interaction.internal.TextInteractions method delete is a built-in method % javahandle.com.mathworks.hg.peer.MenuPeer method delete is a built-in method % javahandle.com.mathworks.hg.peer.ToolbarPeer method delete is a built-in method % javahandle.com.mathworks.hg.peer.ToolPushButtonPeer method delete is a built-in method % javahandle.com.mathworks.hg.peer.ToolToggleButtonPeer method delete is a built-in method % event.ChildEvent method delete is a built-in method % matlab.ui.internal.EmbeddedWebFigureStateManager method delete is a built-in method % mls.internal.FigureUtils method /MATLAB/toolbox/shared/controllib/graphics/+controllib/+widget/+internal/+cstprefs/ToolboxPreferences.m % controllib.widget.internal.cstprefs.ToolboxPreferences method delete is a built-in method % signal.internal.SPTCustomSettings method /MATLAB/toolbox/parallel/cluster/+parallel/@Settings/Settings.m % parallel.Settings method delete is a built-in method % parallel.internal.settings.TypedNodesCollection method /MATLAB/toolbox/parallel/cluster/+parallel/+internal/+settings/NamedNodesCollection.m % parallel.internal.settings.NamedNodesCollection method /MATLAB/toolbox/parallel/cluster/+parallel/+settings/@Profile/Profile.m % parallel.settings.Profile method /MATLAB/toolbox/parallel/cluster/+parallel/+internal/+settings/SettingsNodeFacade.m % parallel.internal.settings.SettingsNodeFacade method delete is a built-in method % parallel.internal.settings.CustomSettingsGetSet method delete is a built-in method % parallel.internal.customattr.CustomPropTypes method delete is a built-in method % parallel.internal.customattr.MetaClass method delete is a built-in method % parallel.internal.customattr.MetaProp method delete is a built-in method % parallel.internal.settings.CustomSettingsPropDisp method delete is a built-in method % parallel.internal.settings.AbstractValidator method /MATLAB/toolbox/parallel/cluster/+parallel/+internal/+settings/NamedNode.m % parallel.internal.settings.NamedNode method delete is a built-in method % parallel.internal.validator.Validator method delete is a built-in method % containers.Map method /MATLAB/toolbox/parallel/cluster/+parallel/+settings/SchedulerComponent.m % parallel.settings.SchedulerComponent method delete is a built-in method % parallel.internal.settings.AbstractNodesCollection method delete is a built-in method % parallel.internal.settings.PoolSettings method delete is a built-in method % event.ClassInstanceEvent method delete is a built-in method % matlab.graphics.primitive.canvas.HTMLCanvas method delete is a built-in method % matlab.graphics.primitive.canvas.HTMLCanvasFactory method delete is a built-in method % schema.class method delete is a built-in method % matlab.io.text.internal.TextSourceWrapper method delete is a built-in method % matlab.io.text.internal.TextParser method delete is a built-in method % matlab.io.internal.meta.FunctionInterfaceMetaProperty method delete is a built-in method % matlab.io.internal.meta.FunctionInterfaceMetaClass method delete is a built-in method % matlab.io.text.internal.TabularTextReader method delete is a built-in method % matlab.io.spreadsheet.internal.Sheet method delete is a built-in method % matlab.internal.doc.services.DocLanguage method delete is a built-in method % FileWrapper__ method delete is a built-in method % PerfTools.Tracer method delete is a built-in method % Simulink.ParameterInterface method delete is a built-in method % Simulink.ConfigSet method delete is a built-in method % Simulink.ConfigSetRoot method delete is a built-in method % Simulink.BaseConfig method delete is a built-in method % Simulink.BaseTemplate method delete is a built-in method % DAStudio.Object method delete is a built-in method % handle.handle method delete is a built-in method % Simulink.TargetCCPropertyAttributes method delete is a built-in method % schema.package method delete is a built-in method % schema.EnumType method delete is a built-in method % schema.type method delete is a built-in method % SlCovCC.ConfigComp method delete is a built-in method % schema.method method delete is a built-in method % Simulink.CustomCC method delete is a built-in method % handle.subreference__ method delete is a built-in method % schema.prop method delete is a built-in method % schema.signature method delete is a built-in method % Simulink.TargetCCProperty method delete is a built-in method % Simulink.ConfigMComponent method delete is a built-in method % Simulink.ConfigComponent method delete is a built-in method % handle.listener method delete is a built-in method % Simulink.Object method delete is a built-in method % Simulink.Root method delete is a built-in method % Simulink.Preferences method delete is a built-in method % Simulink.GeneralPrefs method delete is a built-in method % Simulink.EditorPrefs method delete is a built-in method % Simulink.ModelFilePrefs method delete is a built-in method % Simulink.loadsave.SLXPackageReader method delete is a built-in method % slmsgviewer method delete is a built-in method % Simulink.messageviewer.internal.MsgCache method delete is a built-in method % Simulink.messageviewer.internal.DockedObservers method delete is a built-in method % Simulink.messageviewer.internal.MsgViewerEvents method delete is a built-in method % DAStudio.CustomizationManager method delete is a built-in method % DAS.InterfaceManager method delete is a built-in method % dig.Configuration method delete is a built-in method % mf.zero.Model method delete is a built-in method % mf.zero.ModelElement method delete is a built-in method % mf.zero.PrimitiveSet method delete is a built-in method % mf.zero.PrimitiveSequence method delete is a built-in method % mf.zero.Event method delete is a built-in method % mf.zero.EventRegistration method delete is a built-in method % DAStudio.IconManager method delete is a built-in method % linkfoundation.pjtgenerator.ProcRegistry method delete is a built-in method % linkfoundation.pjtgenerator.AdaptorRegistry method delete is a built-in method % LBCustomizer method delete is a built-in method % realtime.internal.TargetHardware method delete is a built-in method % codertarget.tools.TargetHardwareDeprecationInfo method /MATLAB/toolbox/safety/safety/+safety/+custom/Customizer.m % safety.custom.Customizer method /MATLAB/toolbox/shared/reqmgt/+slreq/+custom/Customizer.p % slreq.custom.Customizer method delete is a built-in method % Simulink.SLTCustomizer method delete is a built-in method % Simulink.harness.HarnessCreateCustomizer method delete is a built-in method % Simulink.CodeCoverageTools method delete is a built-in method % Simulink.ExtModeTransports method delete is a built-in method % rtw.codegenObjectives.ObjectiveCustomizer method delete is a built-in method % Simulink.LookupTableEditorCustomizer method /MATLAB/toolbox/simulink/core/general/+Simulink/InheritRuleCustomizer.p % Simulink.InheritRuleCustomizer method delete is a built-in method % mpt.RTWBuildCustomizer method delete is a built-in method % mpt.MiscCustomizer method delete is a built-in method % coder.targetreg.internal.TargetRegistry method delete is a built-in method % targetframework.internal.repository.TargetRepository method delete is a built-in method % targetframework.internal.repository.datasource.DataSourceDescription method delete is a built-in method % targetframework.internal.repository.datasource.Definitions method delete is a built-in method % codertarget.TargetRegistry method delete is a built-in method % codertarget.TargetBoardRegistry method delete is a built-in method % DVG.Registry method delete is a built-in method % codertarget.TargetListener method delete is a built-in method % RTW.TargetListener method delete is a built-in method % Simulink.Customizer method delete is a built-in method % Simulink.scopes.ViewerLibraryCache method delete is a built-in method % dastudio_util.cooperative.AsyncFunctionRepeaterTask method delete is a built-in method % coderdictionary.data.feature method /MATLAB/toolbox/symbolic/symbolic/@mupadengine/mupadengine.p % mupadengine method delete is a built-in method ...
How can I prove that Value classes require users to build a Destructor function, and users must manually use the Destructor function ‘delete’ to release memory,Otherwise, the Value object‘s memory will be permanently occupied!
Easy. You don't need to explicitly delete a value object and it doesn't permanently occupy memory. You could call clear on it if you really want to force it to Go Away, but you don't have to.
If it were otherwise, creating a double array and passing it into a function that modifies an element of the input (to create a copy in the function's workspace) would cause MATLAB to consume memory at a prodigious rate due to copies in the function workspace piling up. If that were the case I'd expect the code below to take a lot longer to run, as memory gets fragmented by the copies.
x = 1:10;
n = 1e6;
tic
for k = 1:n
y = myfun(x);
end
t = toc;
fprintf("Total time: %g seconds.\nEach call took on average %g seconds.\n", t, t./n)
Total time: 3.5531 seconds. Each call took on average 3.5531e-06 seconds.
Let's look at the variables in the workspace.
whos
Name Size Bytes Class Attributes cmdout 1x33 66 char k 1x1 8 double n 1x1 8 double t 1x1 8 double x 1x10 80 double y 1x10 80 double
clear('y') % Not necessary, but allowed
whos % No more y variable in the workspace
Name Size Bytes Class Attributes cmdout 1x33 66 char k 1x1 8 double n 1x1 8 double t 1x1 8 double x 1x10 80 double
function y = myfun(x)
y = x;
y(1) = NaN;
end
  1 个评论
fa wu
fa wu 2023-8-2
Thanks for your anwser
Which book is that? ---------MATLAB面向对象编程——从入门到设计模式 3.3.7 P92-P94
This document------“A method named delete in a value class is not a destructor. A method named delete in a value class that sets the HandleCompatible attribute to true is not a destructor.”
“A delete method defined by a value class that is handle compatible is not a destructor, even if the delete method is inherited by a handle subclass. For information on handle compatible classes, see Handle Compatible Classes.”
MATLAB will not be able to release the memory of the Value object”-------------I didn't find this in document.but this book said. On the other hand ,I search"value class destructor" just find"Handle Class Destructor" :Search result
I have a question: Answer give an example "double arrays" ,so double arrays is a value class?

请先登录,再进行评论。


Matt J
Matt J 2023-8-2
编辑:Matt J 2023-8-2
How can I prove that Value classes require users to build a Destructor function
You cannot prove this, because it is false. Value classes (as well as handle classes) have their own internal de-allocation mechanisms which will clear the object data from memory automatically when it goes out of scope.
  3 个评论
Matt J
Matt J 2023-8-2
From this blog: "When an instance of a class is cleared, MATLAB automatically cleans up the values of the properties owned by that object."
fa wu
fa wu 2023-8-2
Thanks for your help.
I read this blog.It talk about handle classes . It didn't talk about value classes. Is it?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by