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: 

Why FREE is so underused?

raveeshsaurabh3
Active Participant
0 Kudos

Hello ABAP Gurus,

I had this nagging question for a long time about FREE and CLEAR statements while used to initialize internal tables. I understand that the only difference between them is that FREE releases the complete memory area for the internal table whereas CLEAR reserves the required initial memory area for the internal table.

Throughout my ABAP development career, I have seen CLEAR statement being used mainly, but then FREE can also be used for same purpose, may be even with better performance, as it releases the complete memory.

Can you guys think of something which made people choose CLEAR over FREE? Or my assumption, 'FREE is underused' itself is wrong?

Any comments, ideas are welcome!!

Thanks & Warm Regards,

Raveesh

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

Maybe my blog Releasing Internal Table Memory can also help a bit  ...

24 REPLIES 24

Former Member
0 Kudos

I always used FREE for internal tables instead of refresh and CLEAR for work area/variables.

For me REFRESH only clears the contents of an internal table, FREE - free the memory allocation of the internal table. FREE for me is more efficient if used after program execution.

0 Kudos

REFRESH is obsolete in OO-Context

From SAP documentation:


If the internal table itab has no header line, REFRESH itab acts like CLEAR itab. Since the use of tables with header lines is obsolete and forbidden in classes, the use of REFRESH instead of  CLEAR is also obsolete. To delete rows in internal tables,  CLEAR or FREE can always be used.

0 Kudos

Thanks Suhas for replying!!

My main question was regarding the precedence between CLEAR and FREE. Can you please shed some light on this.

As per my understanding, CLEAR also releases the memory, only difference is that it reserves the initial memory requirement of the internal table. so if we have a an internal table with a lot of data, and we clear clear it, memory gets released except the initial memory requirement, whereas FREE releases all the memory.

Warm Regards,

Raveesh

former_member289261
Active Contributor
0 Kudos

Hi,

FREE is good to use for the internal tables which will no longer be used in the program.

Whereas for the internal tables that will be used frequently CLEAR statement is better for the reason that :

CLEAR : >

Step 1. Clear Data from memory

Step 2. Refill Data to memory

FREE: >

Step 1. Free memory

Step 2. Allocate Memory

Step 3. Fill Data

Former Member
0 Kudos

Hi Raveesh,

As pointed out above, REFRESH is obsolete and I confirmed it doing an F1 on REFRESH and it's covered in Obsolete Internal Table processing section. It was new information for me.

The driving factor behind the difference among them is: efficiency of memory allocation. A developer assigns INITIAL SIZE to an internal table anticipating it to be the most likely the maximum size of the table. The system allocates it and keeps it reserved as long as s/he is manipulating table or even deleting all its rows and the system doesn't need to bother about it. But if the size of your table increases beyond that initial size, it's an overhead. And if you don't need the table any more, you can simply FREE the internal table to save the valuable memory space which the system can allocate to some other resource.

Hope this answers your question.

B/R,

Mukesh

nomssi
Active Contributor
0 Kudos

because the garbage is automatically collected, most of the time you do not need FREE. But then, sometimes you do.

from the ABAP Help:

the garbage collector deletes objects that are no longer referenced by object or data reference variables. The garbage collector is called periodically by the ABAP runtime environment. It follows reference variables of deleted objects. See also weak reference.

JNN

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hmm, the garbage collector for anonymous objects is not dependent on FREE or CLEAR.

The garbage collector steps in when references are initialized and for references CLEAR and FREE do exactly the same. The only difference between CLEAR and FREE is their handling of internal tables regarding the initial memory.

Horst

nomssi
Active Contributor
0 Kudos

Color me confused

anonymous object =

Unnamed data object created using the statement CREATE DATA or the instance operator NEW and which can be addressed using reference variables or field symbols. Anonymous data objects are stored in the heap. An anonymous data object is kept alive by heap references or by field symbols pointing to it.


If an internal table is defined and used in the scope of a method, I do not need to FREE it before the end of the call, do I? And it is NOT an anonymous object, isn't it?


This is what I meant with "most of the time".


The only place I recall using FREE routinely is in ALV Grid implementations (actually it is the free( ) method) because I think the parent will keep a reference and confuse the GC.


JNN


horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

OK, understood.

But removing stack objects from the stack when a method is finished is not exactly garbage collection. We speak of garbage collection for heap objects only.

I guess the question here is more about itabs in (long running) main programs rather than in methods or classes.

Horst

nomssi
Active Contributor
0 Kudos

Hello Horst,


I get your point, but hmm,... I think we cannot use FREE on stack objects?


I understand removing an internal table will always involve GC, as the table body is always on the heap, even if the table reference is on the stack.

JNN

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Jacques,

You are right. The dynamic memory of internal tables, strings and boxed components is located in the heap. An internal table declared on the stack refers to the dynamic data in the heap in the same way as a reference variable refers to anonymous data objects.

So, a statement NEW or CLEAR executed  for an internal table on the stack or the deletion of a stack entry (leaving a method) in fact starts a process that frees the dynamic memory of the table. You can call it GC if you want. Up to now, we spoke of GC only for releasing the heap memory of class instances and anonymous data objects.

Horst

horst_keller
Product and Topic Expert
Product and Topic Expert

Hi Jacques,

It is not the same GC as used for objects but a different process. The GC for objects runs periodically while internal table Memory is released immediately.

Horst

romee_srivastava
Explorer
0 Kudos

Hi Raveesh,

The use of FREE is preferred  when one is sure that the variable will not be used again after that point.

That is why we use CLEAR more.

You might want to read about the usage of FREE statement on this page -

Light Weight ABAP Programming - Code Gallery - SCN Wiki

Regards,

Romee

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

Maybe my blog Releasing Internal Table Memory can also help a bit  ...

0 Kudos

Thats ultimate of course ! My comment above is so insignificant

raveeshsaurabh3
Active Participant
0 Kudos

Hello everyone,

Thanks for your valuable replies. This thread started some serious discussion here.

I am clear with the concept now. Let me summarize once.

CLEAR will be used mainly in case we want to use the internal table further in the program, whereas FREE will be used if we want to get rid of the internal table. This is mainly for global internal tables in main program. Please correct me in case I am wrong here.

What confused me is JNN's remark "I understand removing an internal table will always involve GC, as the table body is always on the heap, even if the table reference is on the stack."

Asp per my understanding, only objects are there on the heap. Can you guys please confirm my understanding here.

Thanks & Warm Regards,

Raveesh

matt
Active Contributor
0 Kudos

Raveesh Saurabh wrote:

Throughout my ABAP development career, I have seen CLEAR statement being used mainly, but then FREE can also be used for same purpose, may be even with better performance, as it releases the complete memory.

Using FREE instead of CLEAR is unlikely to resolve a performance issue.. I use FREE when I no longer need the table but the modular unit I'm currently in continues for a while.

In most cases, unless you're handling huge datasets (most of my programming is in BW, so this is the case), it really doesn't matter if you use FREE or CLEAR - you're not going to run out of memory anyway.

Jelena
Active Contributor
0 Kudos

I use FREE when I no longer need the table but the modular unit I'm currently in continues for a while.

I follow the same principle. If it's the end of the "block" then I just let "god sort 'em out", but if it's in the middle then I use FREE.

If data volume is not large then even not using FREE most likely won't be noticeable. I'm guessing that's why many ABAPers don't bother with it.

I'm curious though if we need to start being more cognizant about the memory usage since HANA runs, you know, in memory.

matt
Active Contributor
0 Kudos

Jelena Perfiljeva wrote:

I'm curious though if we need to start being more cognizant about the memory usage since HANA runs, you know, in memory.

Not really. From the application server it's just another database - the implementation (disk or memory) is irrelevant. HANA runs on its own server with its own memory.

Private_Member_5521
Participant
0 Kudos

Hi Raveesh

Your question has multiple answers, but depends on context.

You know the basic difference between FREE and CLEAR, but the distinction lies on the utility it has to the programmer when taking "architecture-level" decisions.

Let me describe two different scenarios.

CASE 1) The amount of records being read from the database are relatively large, but not exceedingly so, and you are going to be filling and clearing internal tables often while doing your processing.

In this case, CLEAR is a better option, because the hardware has only spent CPU cycles to assign memory. Clearing it just makes its value = empty. At the end of the processing block (be it a method, endform, function or program) that memory will be de-assigned automatically along with everything else that was locally declared so it's not that bad in terms of CPU cycles used.

CASE 2) You are facing some serious data volume, and require multiple steps to accomplish the processing block, even more so, you are faced against the wall of memory capacity of a single processing unit (the amount of RAM space the SAP system can assign to a single user/batch process), as programmers we often try to make those kind of tasks as efficient as possible in terms of execution time, but we also need to be careful not to overflow the memory of the running process

On these cases FREE is a better option because it allows you to manage assigned memory better, however using it too much means wasted CPU cycles in clearing the memory assigned, so you need to be very careful when to use it.


One final thing, the garbage collector takes the dinamically created objects out of memory if they were instanced/created using the CREATE command, But it does not apply to internal tables, unless they are attributes of a class/instanced object.


Hope that helps.

raveeshsaurabh3
Active Participant
0 Kudos

Hello All,

I think the concept is clear here. Thanks so much for your valuable comments. I am not able to assign points here, as I am not getting the option. Can someone help me there, so that I can assign points.

Thanks & Warm Regards,

Raveesh

0 Kudos

You don't get the option because this discussion was not marked as a question. Going forward, please make sure to check the right checkbox (I believe you get a message after submitting the discussion that you have 15 min. to switch the question flag).

In the current situation the best you can do is just click the Like button under the corresponding post(s). I bet Horst Keller here could use a few "pinots" to finally reach the Diamond level.

0 Kudos

Unless of course a moderator switched the question to a discussion to discourage the same answer (and guesses) being given by point hunters.

0 Kudos

Matthew Billingham wrote:

Unless of course a moderator switched the question to a discussion to discourage the same answer (and guesses) being given by point hunters.

That would be me