Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Use of FREE Command

Former Member
0 Kudos
645

Hi,

Can anybody answer pros and cons of using FREE Command?

Regards

Nilesh

8 REPLIES 8

nablan_umar
Advisor
Advisor
0 Kudos
317

Pros: It clears the header of an internal table as well. Refresh command needs Clear to clear the header.

Cons: If your program need to keep the header, then you should use Refresh then.

0 Kudos
317

Hi

By using free, you trigger deallocation. You can use this statement even for global data in your program.

e.g.

DATA gt_itab LIKE ... .

.. ..

PERFORM display_itab .

.. ..

FREE gt_itab .

Freeing 'gt_itab' will be fine after you are done with the itab and no further processing requires that.

For ABAP Objects, there are freeing methods implemented in most classes. However, freeing the referenceexplicitly is required for destruction.

*--Serdar

0 Kudos
317

Serdar's post is correct, though I would like to add a clarification if I may.

Say you have an internal table ITAB with a record length of around 1K, holding around 1,000 rows. If we look at memory allocation simply, we can say that this internal table ITAB occupies about 1MB of memory.

If you use the statement <b>REFRESH ITAB</b> then afterwards the internal table ITAB will be empty, but the internal table will not release the 1MB of memory previously allocated. This can have a performance advantage if your just going to fill the internal table up again, as it re-uses this memory.

If you use the statement <b>FREE ITAB</b> then afterwards, the internal table will be empty but the allocated memory will also be released. Thus if you subsequently repopulate the internal table, the work process will have to reallocate memory again. Using FREE can be particularly important in long-running, high volume processes, when an internal table is no longer required.

FREE originated as a command to manage internal tables. It has subsequently be extended to explicitly destroy class instances in ABAP Objects (as well as OLE2 automation references via FREE OBJECT ...).

Using <b>FREE objref</b> is the same as using <b>CLEAR objref</b>. That is, the variable objref will no longer hold a reference to the class instance. Whether the object is actually destroyed by the garbage collector depends on whether other references to the instance are still held.

Cheers,

Scott

Message was edited by Scott Barden following clarification by Klaus.

0 Kudos
317

One slight clarification:

Using free upon a ABAP object reference frees the refence only. The instance will still exist if referred by any other reference variable.

 free ORef. [Code]  

Freeing an OLE object destroys the OLE instance.

[Code] free object Ole_Ref. 

Kind Regards

Klaus

0 Kudos
317

Thanks for that clarification Klaus. I have updated my posting so as not to mislead.

Cheers,

Scott

Former Member
0 Kudos
317

Thank you for your answers.

so the conclusion is that if you are going to refill the table again in your current session then FREE is not that useful right? But FREE is useful when you are done with using that particular internal table and not going to refill again in your current session.

Regards

Nilesh

0 Kudos
317

Hi Nilesh

The conclusion in a general view is that: It will be always good to make the process deallocate memory for the component when you are finished with it by using FREE statement.

By the way not related to the topic specifically, I guess you are not familiar with the pointing system. In SDN forums, you can reward points to posts which you find helpful. You can do this by pressing the yellow star icon at the header of the post which deserves points. You can reward

- one 10 points (solving answer)

- two 6 points (very helpful answer)

- many 2 points (helpful answer)

Hope to see you here more often

*--Serdar

0 Kudos
317

Nilesh, it is a good practice to use FREE if your program is going to use a lot of memory. If your program is small, quick, and does not use a lot of memory, using Refresh does not harm the system.