Raised This Month: $51 Target: $400
 12% 

Solved Clear() does not free the ArrayList memory in sourcemod?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HarryPotter
Veteran Member
Join Date: Sep 2017
Location: Taiwan, Asia
Old 01-23-2024 , 17:51   Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #1

ArrayList.Clear Method
PHP Code:
test.sp

ArrayList g_aArrayList
;
public 
void OnPluginStart()
{
    
g_aArrayList = new ArrayList();

    for(
int i 0<= 1000000i++)
    {
        
g_aArrayList.Push(i);
    }
    
    
g_aArrayList.Clear();

    
ServerCommand("sm_dump_handles test.cfg");

You can see it will not FREE the memory
Code:
Handle    	Owner               	Type                	Memory    	Time Created                  
---------------------------------------------------------------------------------------------
0x6410051c	test.smx            	CellArray           	4194324   	01/24/2024 - 06:40:50


CloseHandle Method
PHP Code:
test.sp

ArrayList g_aArrayList
;
public 
void OnPluginStart()
{
    
g_aArrayList = new ArrayList();

    for(
int i 0<= 1000000i++)
    {
        
g_aArrayList.Push(i);
    }

    
delete g_aArrayList;
    
g_aArrayList = new ArrayList();

    
ServerCommand("sm_dump_handles test.cfg");

It will FREE the memory
Code:
Handle    	Owner               	Type                	Memory    	Time Created                  
---------------------------------------------------------------------------------------------
0x6420051c	test.smx            	CellArray           	20        	01/24/2024 - 06:47:20
Same thing happens to StringMap, ArrayStack I believe

Edit: Solved, See #7 Reply
__________________

Last edited by HarryPotter; 02-06-2024 at 18:00.
HarryPotter is offline
LinLinLin
Senior Member
Join Date: Sep 2021
Old 01-23-2024 , 21:37   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #2

look like it just only set the size to zero but not free the data.
In CellArray.h

PHP Code:
CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
    {
    }

    ~
CellArray()
    {
        
free(m_Data);
    }

    
/**
    * @brief Creates a cell array object.
    *
    * @param blocksize  The number of cells each member of the array can
    *                   hold.  For example, 32 cells is equivalent to:
    *                   new Array[X][32]
    * @return            A new ICellArray object.
    */
    
static ICellArray *New(size_t blocksize)
    {
        return new 
CellArray(blocksize);
    }

    
/**
    * @brief Releases a cell array's resources.
    *
    * @param pack        An ICellArray object to release.
    */
    
static void Free(ICellArray *arr)
    {
        
delete arr;
    }

    
// ICellArray
public:
    
size_t size() const
    {
        return 
m_Size;
    }
/*...*/
    
void clear()
    {
        
m_Size 0;
    } 
in smn_adt_array.cpp
PHP Code:
static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
{
    
CellArray *array;
    
HandleError err;
    
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);

    if ((
err handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
        != 
HandleError_None)
    {
        return 
pContext->ThrowNativeError("Invalid Handle %x (error: %d)"params[1], err);
    }

    array->
clear();

    return 
1;

maybe those memory are reserved after create so that it doesn't need create again to hold the new data.

Last edited by LinLinLin; 01-23-2024 at 21:43.
LinLinLin is offline
HarryPotter
Veteran Member
Join Date: Sep 2017
Location: Taiwan, Asia
Old 01-24-2024 , 17:51   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #3

Quote:
Originally Posted by LinLinLin View Post
maybe those memory are reserved after create so that it doesn't need create again to hold the new data.
Then I guess programmer has to consider delete instead of Clear() everytime to prevent from high memory usage
__________________

Last edited by HarryPotter; 01-25-2024 at 16:12.
HarryPotter is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 01-25-2024 , 08:49   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #4

Good to know, thank you for the info, going to apply this on my future plugins updates.
Would be good if they could create some wrapper that do both Clear and delete as well.
__________________
Marttt is offline
3ipKa
Member
Join Date: Jul 2013
Old 01-28-2024 , 09:04   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #5

So, have I to delete a global array every time and create it again and again!?
3ipKa is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 01-28-2024 , 09:19   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #6

Quote:
Originally Posted by 3ipKa View Post
So, have I to delete a global array every time and create it again and again!?
If you create adt_array, stringmap or similiar, what return a handle.
- you can use it long time and clear it when need.
- if you create new adt_array what return new handle for same purpose, delete old handle also, or those remain in memory until plugin unload.

sm_dump_handles handles.txt


https://wiki.alliedmods.net/Handles_...losing_Handles
__________________
Do not Private Message @me

Last edited by Bacardi; 01-28-2024 at 09:22.
Bacardi is offline
Earendil
Senior Member
Join Date: Jan 2020
Location: Spain
Old 02-05-2024 , 14:58   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #7

Spoiler


All those things you are mentioning are Objects, once initialized with "new" a memory space is asigned to the object, using clear() will not free that memory space since objects still exist even if its content is removed.

Using delete causes to remove the assigned memory space, freeing it, but at a cost that you are required to initialize them again.

If you need to empty/fill an ArrayList multiple times, is better to use clear because is faster to skip the memory assignment. In the other hand is better to destroy objects and then initialize them if the data they are storing is big enough and is not required again.

Since SP has no garbage collector, allways delete handles once you don't need to use them anymore and always delete handles once you ended them in a code block. If not you will have memory leaks that will only be cleared after plugin unload.

And yeah, SourcePawn is not Object Oriented, but I imagine the lower layers of SM will work with objects, and ArrayLists and the other things are basically SM objects that we access via Handles.
__________________
>>My plugins<<
>>GitHub<<
Earendil is offline
HarryPotter
Veteran Member
Join Date: Sep 2017
Location: Taiwan, Asia
Old 02-06-2024 , 18:01   Re: Clear() does not free the ArrayList memory in sourcemod?
Reply With Quote #8

Quote:
Originally Posted by Earendil View Post

All those things you are mentioning are Objects, once initialized with "new" a memory space is asigned to the object, using clear() will not free that memory space since objects still exist even if its content is removed.

Using delete causes to remove the assigned memory space, freeing it, but at a cost that you are required to initialize them again.

If you need to empty/fill an ArrayList multiple times, is better to use clear because is faster to skip the memory assignment. In the other hand is better to destroy objects and then initialize them if the data they are storing is big enough and is not required again.

Since SP has no garbage collector, allways delete handles once you don't need to use them anymore and always delete handles once you ended them in a code block. If not you will have memory leaks that will only be cleared after plugin unload.

And yeah, SourcePawn is not Object Oriented, but I imagine the lower layers of SM will work with objects, and ArrayLists and the other things are basically SM objects that we access via Handles.
Thank you, good to know, I will be more careful in the future.
__________________

Last edited by HarryPotter; 02-06-2024 at 18:02.
HarryPotter is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode