1

Is it possible to deallocate a function? Say you have a huge initialisation function that only gets run once, is it possible to deallocate it after you called it, and use that extra storage? I'm mainly interested in C, but feel free to post up for any other language if that language offers interesting behaviour.

I understand that functions aren't allocated on the heap and thus you probably cannot recycle their memory, but this does seem like a waste to me.

Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • Not in standard C. (Remember that the C language doesn't have any concept of heaps, stacks, or where functions "live".) – Oliver Charlesworth May 17 '14 at 12:52
  • @OliCharlesworth I'm assuming you mean heap etc is implementation dependant? Also _"not in standard C"_, how about non standard C? – Jeroen May 17 '14 at 12:53
  • Yes, exactly. It's entirely possible that you'd be able to find a non-standard way of doing this on whichever platform you're operating on, though. – Oliver Charlesworth May 17 '14 at 12:53
  • It is not an answer, but I would recommend buying more memory. Your huge function may have e.g. 1000 lines = 2 000 bytes compiled. Don't know which platform you are working on, but 2 kB of memory shouldn't be that expensive. – Ivan Kuckir May 17 '14 at 12:58
  • How large is your function?!?! – Kerrek SB May 17 '14 at 12:59
  • 2
    You can of course do some kind of dynamic loading and unloading, which is supported by most modern desktop platforms... – Kerrek SB May 17 '14 at 12:59
  • @IvanKuckir This is hypothetical, I don't have a function written, I'm interested if this is possible and how it would work. – Jeroen May 17 '14 at 13:00
  • @KerrekSB I'm not having memory shortage. I'm just exploring possibilities. – Jeroen May 17 '14 at 13:01
  • Alright, fine, but please realize that this is a complete non-issue. – Kerrek SB May 17 '14 at 13:02
  • It is like asking "Is it possible to allocate an array of length 0". There is no use in doing such things, so language authors omit them and we should be grateful for that. – Ivan Kuckir May 17 '14 at 13:03
  • @IvanKuckir I argue there is no use in keeping a function that won't even be ran again. – Jeroen May 17 '14 at 13:06
  • @KerrekSB I do realise, I'm just interested. – Jeroen May 17 '14 at 13:06

4 Answers4

1

There is no way to do so using the language features.

But depending on the system you work on, the OS might do that on its own by the normal memory management techniques. (But if so, then by "accident".)

Let me explain a bit: the program gets mapped into memory and run. Code which hasn't been used for a certain time will get thrown out of memory. Data is put into the swap area on disk in order to be able to "recycle" it later, but code is always present in the executable file and can be fetched from there if needed again.

Of course, this does not happen on all systems, but Linux and AFAIK Windows as well works this way.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • This is very interesting, but still I'd like to see if there are ways to do this in any language. – Jeroen May 17 '14 at 12:59
  • @JeroenBollen: You seem to be conflating "language" with "implementation". It is unlikely that there are any languages that have first-class support for this sort of implementation detail (although it's difficult to prove a negative), as it may be compiled to run on platforms where such a concept doesn't even make sense. However, on *particular* systems, there may be an API available that allows you to manipulate how memory is being used. – Oliver Charlesworth May 17 '14 at 13:13
  • @OliCharlesworth which brings me to the question, why would no language (or implementation of said language) allow for such functionality? – Jeroen May 17 '14 at 16:32
  • @Jeroen: (speculating) because (a) there's usually no need, and (b) because unless a language is tied to a particular platform, it may get run on platforms where the concept isn't even possible. – Oliver Charlesworth May 17 '14 at 17:20
  • @OliCharlesworth An implementation is platform specific though, and I agree regular applications don't need this. – Jeroen May 17 '14 at 17:23
1

With python you can create classes and functions during the lifetime of the program, and also remove them. You lose memory due to the fact that there's some boilerplate on top of the implementation language (cPython uses C) though, so it wouldn't be a particularly useful concept in this application

Angus Hollands
  • 351
  • 2
  • 11
  • How is it implemented? – Jeroen May 17 '14 at 15:54
  • A quick note, whilst I am relatively confident in my advice, some of the CPython codebase I've not experienced, so I'm inferring from the language. Python is interpreted in most implementations. CPython will produce byte code and interpret that on the fly: http://stackoverflow.com/a/1644742/1215241 It uses memory management - garbage collection, and everything is an object. Hence, functions are objects, and as such can be dynamically added and removed from the language, once all references are omitted. To ensure that you delete your init function, ensure all but one reference are weakrefs – Angus Hollands May 17 '14 at 16:29
  • This might be an interesting read http://stackoverflow.com/questions/11016078/is-it-possible-to-create-a-function-dynamically-during-runtime-in-c?rq=1 – Angus Hollands May 17 '14 at 16:35
  • I know you can compile a function and load it as a dynamic library, but that's not really related to this question. – Jeroen May 17 '14 at 16:39
  • In this context, it's not the same as loading a dynamic library, it's an interpreted language which gives it this feature. In your case, if you can find where a function is defined in memory, and any pointers to it, and safely delete them, you've achieved your goal, but that memory is likely unusable by the program, hence it's idle "waste" that cannot be used for what it's designed for. Interpreted languages are not (typically) compiled into memory, so the data can be removed somewhat as easily as appended into memory. – Angus Hollands May 17 '14 at 19:58
  • For example, CPython hasn't really a concept of stack/heap allocation, everything is a PyObject instance, and everything is allocated (in CPython) on the heap. Might want to read this, for curiosities sake! http://www.troeger.eu/files/teaching/pythonvm08.pdf My point is that it is possible, Python is one language of a number which enables you to create object types on the fly, and hence has this flexibility – Angus Hollands May 17 '14 at 20:03
  • Python is interpreted though, which isn't particulary hard to edit at runtime. I'm talking about low level languages like C and D. – Jeroen May 17 '14 at 20:28
0

You can do that with shared libs (eg dll). Use them and unload them...

  • I know I can do that, and the function loading the DLL would be a perfect function I would want to unload. – Jeroen May 18 '14 at 08:47
0

You can define functions at runtime using JavaScript and assign them to variables. Later you can delete the function or just leave it to the garbage collector when defined as local variables.

var MyImplementation = {};
MyImplementation.test = function() {
console.log("hello");
};
MyImplementation.test();
delete MyImplementation.test;
MyImplementation.test();

This produces:

hello

TypeError: undefined is not a function

Community
  • 1
  • 1
illfang
  • 65
  • 1
  • 4