raydium-io / raydium-sdk-V2-demo

Open-source Typescript SDK demos
48 stars 31 forks source link

How to completely destroy a Raydium instance? #84

Closed liu798675179 closed 2 weeks ago

liu798675179 commented 2 weeks ago

Define the raydium instance in a function, which is used for swap transactions. Use load() to initialize the instance and pass in the owner field. The instance is not destroyed after the function is run. Account information is always requested regularly. When the function is called multiple times, the network will be congested and the memory will continue to accumulate.

After using the raydium.account.updateTokenAccount(await fetchTokenAccountData()) method, the network congestion is resolved, but the memory is still slowly increasing, and it feels like there are still instances that have not been destroyed.

@cruzshia @rudy5348 help me check it please, thanks.

const raydium = await Raydium.load({
        connection,
        owner: ownerKeypair,
        cluster,
        disableFeatureCheck: true,
        disableLoadToken: true,
        blockhashCommitment: "finalized",
      });

// other codes
// ......

try {
          raydium.account.updateTokenAccount(
            await this.fetchTokenAccountData(ownerKeypair.publicKey)
          );

          connection.onAccountChange(ownerKeypair.publicKey, async () => {
            if (raydium && raydium.account) {
              raydium.account.updateTokenAccount(
                await this.fetchTokenAccountData(ownerKeypair.publicKey)
              );
            }
          });
        } catch (e) {
          console.log("raydium error", e);
        } finally {
          if (raydium) {
            raydium.owner = null;
            raydium = null;
          }

          if (connection) {
            connection = null;
          }
}
cruzshia commented 2 weeks ago

Sdk doesn't run any jobs in background and due to javascript's garbage collection mechanism, we don't provide destroy function that js will release memory automatically if variables/objects are not used anywhere.

liu798675179 commented 2 weeks ago

Sdk doesn't run any jobs in background and due to javascript's garbage collection mechanism, we don't provide destroy function that js will release memory automatically if variables/objects are not used anywhere.

Yeah, that's the weirdest part. Let me monitor this carefully for a while longer.

liu798675179 commented 2 weeks ago

When I comment out the business code in the function and only keep the initialization code of Raydium, and call the function regularly, the memory also increases slowly. When I comment out the relevant code of Raydium and call the function regularly, the memory tends to be stable. So it is likely that Raydium has similar scheduled tasks, event monitoring, closures, etc. after initialization? So it has been occupying resources and cannot be destroyed?

cruzshia commented 2 weeks ago

the only thing SDK might have is only watching token account changes here https://github.com/raydium-io/raydium-sdk-V2/blob/master/src/raydium/account/account.ts#L124, nothing else. if you don't need sdk watch token account change event, you can manage token account by yourself, example: updateTokenAccount and raydium UI also uses SDK v2 and don't have such problem

cruzshia commented 2 weeks ago

SDK code is open source you can review it (initialize code here: https://github.com/raydium-io/raydium-sdk-V2/blob/master/src/raydium/raydium.ts#L154), there's no jobs run inside , and if you also can check your code whether access data from sdk might cause closures, thx